zsh PATH 的奇怪行为

Weird behaviour with zsh PATH

我今天只是鼓励 zsh 出现一个奇怪的问题。

我的环境是MacOSXYosemite,zsh 5.0.5(x86_64-apple-darwin14.0)

在 .zshrc 中,我手动将 PATH 变量设置为类似

export PATH="$PATH:~/.composer/vendor/bin"

在终端中尝试echo $PATH,结果如预期(包含~/.composer/vendor/bin)。然后尝试从 ~/.composer/vendor/bin 执行二进制文件,它总是 return 我 "zsh: command not found" 错误。

尝试切换到bash,echo $PATH也符合预期,与zsh shell的结果相同。 尝试从 ~/.composer/vendor/bin 执行二进制文件,没有发现问题。似乎 PATH var 在 bash shell.

上表现良好

我的 zsh shell 怎么了?

谢谢

键入 rehash 以获取 $PATH 更改。

来自zsh user guide

The way commands are stored has other consequences. In particular, zsh won't look for a new command if it already knows where to find one. If I put a new ls command in /usr/local/bin in the above example, zsh would continue to use /bin/ls (assuming it had already been found). To fix this, there is the command rehash, which actually empties the command hash table, so that finding commands starts again from scratch. Users of csh may remember having to type rehash quite a lot with new commands: it's not so bad in zsh, because if no command was already hashed, or the existing one disappeared, zsh will automatically scan the path again; furthermore, zsh performs a rehash of its own accord if $path is altered. So adding a new duplicate command somewhere towards the head of $path is the main reason for needing rehash.

编辑 但是@WilliamPursell 的评论可能会有所作为:

note that "composer" != ".composer"

尝试使用 $HOME 而不是 ~。在许多情况下,shell 不会像您期望的那样扩展 ~,通常最好使用 $HOME~ 实际上只是作为交互使用的捷径。 (我能记得的唯一情况是 ~ 是首选的是在 .gitalias 中,其中 ~ 被扩展而变量没有。)