当运行zsh -l -c' echo" __ $ {PATH} __"'时,virtualenv目录转移到$ PATH的末尾。

时间:2014-08-27 04:28:53

标签: python macos shell virtualenv zsh

为什么virtualenv路径在最后一个命令中移到了最后?

Last login: Tue Aug 26 19:14:49 on ttys000
➜  ~  echo $PATH
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
➜  ~  zsh -l -c 'echo "__${PATH}__"'
__/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin__
➜  ~  . ./venvs/py_venv/bin/activate
(py_venv)➜  ~  echo $PATH
/Users/aj/venvs/py_venv/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
(py_venv)➜  ~  zsh -l -c 'echo "__${PATH}__"'
__/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/aj/venvs/py_venv/bin__
(py_venv)➜  ~

修改

进一步调查似乎表明它与单引号和双引号有关。仍不确定原因:

(py_venv)➜  ~  zsh -l -c "echo $PATH"
/Users/aj/venvs/py_venv/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
(py_venv)➜  ~  zsh -l -c 'echo $PATH'
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/aj/venvs/py_venv/bin

2 个答案:

答案 0 :(得分:0)

在我的( Ubuntu zsh 环境中,我没有看到行为上的差异,但我怀疑你的情况(可能与 OS X )它可能与双引号允许完全替换这一事实有关,而单引号则不然。

例如,如果运行echo '$PATH',它将输出$PATH,而echo "$PATH"将环境变量的值替换为echo命令并输出完整路径。 / p>

Further reading on zsh quoting

修改

我尝试了一些你的确切zsh命令的变体,并且可以重现类似的东西,虽然我最终没有输出而不是它转移到最后:

 (venv_test) ~  zsh -l -c 'echo "__$PATH__"'
__
 (venv_test) ~  zsh -l -c 'echo __$PATH__' 
__
 (venv_test) ~  zsh -l -c "echo __$PATH__"
__
 (venv_test) ~  zsh -l -c "echo __${PATH}__"
__/home/khampson/virtualenvs/venv_test/bin:/home/khampson/bin__

尝试在env var周围使用花括号切换到双引号(我怀疑尾部下划线被视为env var名称的一部分)。

答案 1 :(得分:0)

来自zsh手册页:

STARTUP/SHUTDOWN FILES
       Commands are first read from /etc/zshenv; this cannot be overridden....

所以,让我们默认看看Mac OS X在那里有什么:

➜  ~  cat /etc/zshenv
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

好的,/usr/libexec/path_helper是什么?

➜  ~  man path_helper
DESCRIPTION
     The path_helper utility reads the contents of the files in the directories /etc/paths.d and /etc/manpaths.d and appends their contents to the PATH and MANPATH environment variables respectively.

因此,如果path_helper 附加 ,那么为什么将venv目录推送到最后?答案是它没有附加,它预先添加,文档不正确。它似乎也在做一些重复推理:

(py_venv)➜  ~  echo $PATH
/Users/aj/venvs/py_venv/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
(py_venv)➜  ~  /usr/libexec/path_helper -s
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/aj/venvs/py_venv/bin"; export PATH;
(py_venv)➜  ~  PATH="" /usr/libexec/path_helper -s
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin"; export PATH;
(py_venv)➜  ~  PATH="/foo/bar" /usr/libexec/path_helper -s
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/foo/bar"; export PATH;
(py_venv)➜  ~  PATH="/foo/bar:/usr/bin:/usr/local/bin" /usr/libexec/path_helper -s
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/foo/bar"; export PATH;
(py_venv)➜  ~