4

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?

terdon
  • 242,166
Incerteza
  • 2,671

1 Answers1

7

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.

goldilocks
  • 87,661
  • 30
  • 204
  • 262
  • and what do I do? – Incerteza Jul 14 '14 at 12:05
  • Use export PATH=$HOME/.cabal/bin:$PATH -- with $PATH after $HOME/.cabal/bin. In case it's not clear, this works by concatenating the old value of $PATH with some new value(s). Analogy: WORD=bar; WORD=foo$WORD -> $WORD is now foobar. WRT $PATH, don't forget the colon in between. – goldilocks Jul 14 '14 at 12:18
  • and how do I remove the one I've already added? – Incerteza Jul 14 '14 at 12:58
  • In other words, how do I redo this export PATH=$PATH:$HOME/.cabal/bin ? – Incerteza Jul 14 '14 at 12:59
  • 1
    If you did it on the command line, you can just exit the shell/terminal you're using -- export applies it to the current shell and all its children, not everything that's running. If you put it in .bash_profile or something, just change it and log in again. In any case, it's harmless to add the path at the beginning and leave it at the end as well. – goldilocks Jul 14 '14 at 13:10
  • so running this in the terminal doesn't make it persistent? Where do I have to add the line export PATH=$HOME/bin:$PATH - in the beginning or in the end of .bash_profile? – Incerteza Jul 14 '14 at 13:57
  • @Alex Yes, that would work. – v010dya Jul 14 '14 at 14:43
  • @Volodya, this isn't a yes/no question. – Incerteza Jul 14 '14 at 14:53
  • Put it at the end if there is anything predefined in that file. If it doesn't work after you log in, you'll need to source it from an .xsession file or put it in .bashrc, in which case you may want to read this: http://unix.stackexchange.com/questions/124444/how-can-i-cleanly-add-to-path – goldilocks Jul 14 '14 at 14:55
  • 1
    ...the issue is that some graphical login systems ("display managers") don't source .bash_profile, but they do use .xsession -- so if you source ~/.bash_profile in ~/.xsession it will work. http://unix.stackexchange.com/questions/47359/what-is-xsession-for – goldilocks Jul 14 '14 at 14:59