问题
How is it possible that the binary executed 'by default' -- ie. the first one found in my path, as returned by the which
command, is in fact not the binary actually being executed by default? (I'm running Mac OS X 10.5 on a 2008 MacBook Pro.)
I just installed the latest version of git by running brew install git
It installed the new version in a location higher or earlier in my path than the old binary. However, the new version is not executed by default. It's as if the shell had cached the location of the binary before I installed the new one - but the which
command parses the path every time it runs.
I'd like to understand what's going on here and learn how to flush the 'cache' without having to log out or restart the machine. This is what I'm seeing:
which git
returns
/usr/local/bin/git
while
git --version
returns
git version 1.6.5
but
/usr/local/bin/git --version
returns
git version 1.7.7
Checking further,
which -a git
returns
/usr/local/bin/git
/usr/local/git/bin/git
/usr/local/bin/git
/usr/local/git/bin/git
/usr/local/bin/git
/usr/local/git/bin/git
(yeah - there's some redundancy in my path setup.)
/usr/local/git/bin/git --version
returns
git version 1.6.5
UPDATE -- here's the answer bash hashes. (from: In bash, "which" gives an incorrect path - Python versions )
type git
git is hashed (/usr/local/git/bin/git)
$ hash -d git
$ type git
git is /usr/local/bin/git
$ which git
/usr/local/bin/git
git --version
git version 1.7.7
回答1:
Within a given instance of the shell, paths for binaries are indeed cached. The simplest way to clear this is to just open a new terminal window. But you can clear it in an existing window too by using the hash
shell built-in (read help hash
for details).
As for why which
disagrees with the shell about the binary to execute, it's because which
is a program that lives at /usr/bin/which
and parses the PATH
independently of the shell. If you want to see exactly what the shell is seeing, use type
instead of which
(and use type -a
to see all possible results for a given command).
来源:https://stackoverflow.com/questions/7814312/why-or-how-is-it-that-is-my-path-is-not-being-respected-macosx