How can `which cmd` != cmd?

Can anyone explain this?

$ vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Jul 12 2010 12:47:31)
$ `which vim` --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 13 2011 22:52:17)

I just compiled the latest version of vim and installed it to $HOME/bin, which is the first path in $PATH. which vim returns the correct path: /home/user/bin/vim, however, when I execute vim it launches /usr/bin/vim.

I launched another xterm and the problem has disappeared, however I am still curious as to how the previous term got into such a weird state. Any ideas?


Solution 1:

In UNIX, when you run a command, the shell saves the path where it found the binary. If you then change the path, or install a different version in directory earlier in the path, then the shell will not find it because it does not actually search for commands that it has already found.

If you had typed hash -r before comparing which vim and vim, then you would not have seen this discrepancy.