Saturday, March 26, 2005

Unix PATH overload

Many of us, customize our shells under Unix (for the difference between Unix and UNIX - see the Art of Unix Programming). We usually add to our PATH environment variable. Usually what we do is

PATH=$PATH:[paths to add]

Lets assume this file is called .profile and is read by the shell on start-up. This approach is good, but if the user were to change our .profile and run

. $HOME/.profile

Then on seeing the PATH variable, it would have repeated path names.

If the change to PATH was

PATH=$PATH:$HOME/bin:.

The on running . $HOME/.profile for the first time the user would see PATH as

[original]:[user's home directory]/bin:.:[user's home directory]/bin:.


One easy way to work around this problem is to use the following approach in your .profile

OLDPATH=${OLDPATH=$PATH}
export OLDPATH
export PATH=
export PATH=$OLDPATH:/sbin:/usr/sbin:$HOME/bin:.

and then customize PATH as the user did previously, repeated running of
. $HOME/.profile shall now not cause the PATH environment variable to grow uncontrollably

No comments:

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...