자식 프로세스에 별칭 전달

자식 프로세스에 별칭 전달

/etc/zprofile/etc/profile(또는 bash)에 몇 가지 별칭이 정의되어 있습니다 . 거기도 $PATH조작됐어요.

작업을 피하는 쉘에서 대화형 쉘을 시작하고 싶습니다 $PATH. -f옵션을 사용하여 실행하면 됩니다 . 그러나 상위 셸에 정의된 별칭은 유용하므로 이를 하위 프로세스에 전달하고 싶습니다.

한 번 정의된 별칭을 텍스트 파일에 복사하여 붙여넣고 다른 셸을 시작할 때마다 이를 가져오는 것은 정의가 유지되고 시간이 지남에 따라 변경될 수 있기 /etc/zprofile때문에 내가 원하는 것이 아닙니다 ./etc/zprofile

별칭을 두 번째 셸로 전달하는 방법에 대한 제안 사항이 있습니까?

(저의 주된 관심은 솔루션이지만, zsh솔루션도 궁금합니다)bash

상황이 이렇다고 봅니다

# log in with ssh
shell1 > echo "here aliases are the way I want them to be"
shell1 > VARIABLE1=value1 VARIABLE2=value2 PATH=some_paths zsh
shell2 > echo "/etc/zprofile prepended something to PATH, which I don't like"
shell2 > echo "aliases are good"
shell2 > exit
shell1 > VARIABLE1=value1 VARIABLE2=value2 PATH=some_paths zsh -f
shell2 > echo "/etc/zprofile did not prepend something to PATH. This is good!"
shell2 > echo "but all aliases from shell 1 are \"forgotten\""
shell2 > exit

정보를 계속 설명하면 /etc처음에 생각했던 것보다 내용이 더 복잡해집니다. /etc/zprofile다음 내용이 포함되어 있으며, 이를 추적하여 내 별칭을 정의했습니다.$PATH

_src_etc_profile()
{
    #  Make /etc/profile happier, and have possible ~/.zshenv options like
    # NOMATCH ignored.
    #
    emulate -L ksh

    # source profile
    if [ -f /etc/profile ]; then
        source /etc/profile
    fi
}
_src_etc_profile

unset -f _src_etc_profile

/etc/zshrc하지만 그 내용이 포함되어 있다는 사실을 놓쳤습니다

_src_etc_profile_d()
{
    #  Make the *.sh things happier, and have possible ~/.zshenv options like
    # NOMATCH ignored.
    emulate -L ksh


    # from bashrc, with zsh fixes
    if [[ ! -o login ]]; then # We're not a login shell
        for i in /etc/profile.d/*.sh; do
        if [ -r "$i" ]; then
            . $i
        fi
        done
        unset i
    fi
}
_src_etc_profile_d

unset -f _src_etc_profile_d

그래서 그것은 zprofile통과 할 뿐만 아니라 /etc/profile.d; zshrc파일 중 하나 /etc/profile.d는 다음과 같습니다(어떻게 진행되는지에 대한 설명을 추가했습니다).

if [ -n "$SHLVL" ]; then
    if [ $SHLVL = 1 ]; then
            source /some/group/path/profile
            # calls "source /some/group/path/env"
            # -> see else branch
    else
            source /some/group/path/env
            # calls "source /some/group/path/${SHELL}rc"
            # which then calls
            # "source /my/group/path/group_zshrc"
    fi
else
    echo 'No $SHLVL available, assuming login shell.'
    source /some/group/path/profile
fi

/my/group/path/group_zshrc임시 파일에 쓰고 별칭과 환경 변수를 정의/조작한 다음 마지막으로 임시 파일을 가져오는 Python 프로그램을 호출 하게 됩니다 .

답변1

/etc/zprofile또는 에 별칭을 정의 하면 안 됩니다 ~/.zprofile. 이러한 파일은 로그인 셸(물론 "shell"은 zsh를 나타냄)에서만 로드됩니다. 별칭을 정의하는 올바른 위치는 /etc/zshrc또는 입니다 ~/.zshrc. 모든 대화형 쉘은 이를 읽습니다.

로딩 없이 zsh를 실행하려면 옵션 없이 실행 /etc/zprofile하면 됩니다 . zsh이 옵션은 -fzsh에게 구성 파일을 전혀 읽지 않도록 지시하지만 별칭 정의를 읽으려고 하므로 이는 올바른 옵션이 아닙니다. /etc/zprofile로그인 쉘에서만 읽을 수 있으므로아니요옵션을 전달합니다 -l(그리고 대시로 시작하는 0번째 인수를 설정하지 마세요).

Zsh에는 시작 시 추가 파일을 읽을 수 있는 옵션이나 환경 변수가 없습니다. 이에 대비하고 싶다면 이를 eval $ZSH_EXTRA_STARTUP_CODE에 추가할 수 있습니다 .zshrc.

read 및 ZDOTDIR와 같은 다양한 디렉터리에서 모든 사용자 구성 파일을 읽도록 환경 변수를 설정할 수 있습니다 . 따라서 현재 인스턴스의 별칭을 하위 인스턴스로 내보내려면 다음과 같이 실행할 수 있습니다.ZDOTDIR=/tmp/foo zsh/etc/zshenv/tmp/foo/.zshenv/etc/zshrc/tmp/foo/zshrc

tmp=$(mktemp -d)
alias -L >$tmp/.zshenv
echo ". ~/.zshenv; unset ZDOTDIR; rm -r ${(q)tmp}" >>$tmp/.zshenv
zsh

답변2

존재하다bash

이 옵션을 다음과 같이 남용 할 수 있습니다 --rcfile.

bash --noprofile --rcfile <(alias)

그러나 이를 쉘로 사용할 수 있는 옵션이 없으면 --noprofile쓸모가 없습니다.-llogin

관련 정보