Shell – Keep getting the same result after “export PATH”

environment-variablespathshell

I am doing this:

$ which cabal
/usr/bin/cabal
$ export PATH=$PATH:$HOME/.cabal/bin
$ which cabal
/usr/bin/cabal

I expect to get /.cabal/bin/cabal for $ which cabal (this path exists) after this. But I don't even after re-opening the terminal. How come?

Best Answer

The paths in $PATH are searched in order. This allows you to override a system default with:

export PATH=$HOME/bin:$PATH

$HOME/bin is now the first (highest priority) path. You did it the other way around, making it the last (lowest priority) path. When the shell goes looking, it uses the first match it finds.

In case it's not clear, this all works by concatenating strings. An analogy:

WORD=bar
WORD=foo$WORD

$WORD is now foobar. The : used with $PATH is literal, which you can see with echo $PATH.

Related Question