MacOS – Why “which” command does not give you the correct path

macos

See the following mvn version. I'm wondering why "which" command does not give you the correct info.

LM-SIN-00676037:myproject haibliu$ mvn --version
Apache Maven 3.0.3 (r1075438; 2011-03-01 01:31:09+0800)
Maven home: /usr/share/maven
Java version: 1.6.0_37, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.8", arch: "x86_64", family: "mac"

LM-SIN-00676037:myproject haibliu$ which mvn
/usr/local/bin/mvn

LM-SIN-00676037:myproject haibliu$ /usr/local/bin/mvn --version
Apache Maven 3.0.4 (r1232337; 2012-01-17 16:44:56+0800)
Maven home: /usr/local/Cellar/maven/3.0.4/libexec
Java version: 1.6.0_37, vendor: Apple Inc.
Java home: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
Default locale: en_US, platform encoding: MacRoman
OS name: "mac os x", version: "10.8", arch: "x86_64", family: "mac"

Just to clarify:
/usr/bin/mvn comes with my Max OS X, Mountain Lion. And I used Homebrew to install /usr/local/bin/mvn. After that, I changed the order of PATH in /etc/paths.

LM-SIN-00676037:myproject haibliu$ cat /etc/paths
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
LM-SIN-00676037:myproject haibliu$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

That's all I did. And I'm using bash shell.

Best Answer

which is an external program, which tries to determine how your shell will resolve a command from the existing $PATH, but it is possible for it to get it wrong.

If you're using bash as your shell, use the bash built-in command "type" to have the shell itself tell you how it will resolve that command:

$ which mvn
/usr/bin/mvn
$ type mvn
mvn is /usr/bin/mvn
$ type type
type is a shell builtin
$ type which
which is hashed (/usr/bin/which)

You can see here that in my environment that /usr/bin/which and the shell built-in type agree. In yours it seems they don't, though I am curious why they don't. Perhaps you have PATH changes that were not exported? Try "export PATH" and then "which mvn" again.