Rereading .bash_profile

Lew Bloch conrad at lewscanon.com
Mon Oct 25 21:36:29 UTC 2004


"Robert P. J. Day" wisely reminded us:
> a classic example is to append some entries to your search path:
> 
>   PATH=$PATH:blah:woof:.....
> 
> rerunning .bash_profile means that those entries will be appended
> *again*.  not fatal, of course, but i think too many people very
> cavalierly re-source that file without making sure there are no
> adverse consequences.

With care you can ensure that your .bash_profile actions are idempotent 
- they leave the same result state as the first time every time.

For example:

     if [ "${PATH/\/blah\/bin/}" = "${PATH}" ]
     then
	PATH=${PATH}:/blah/bin
     fi

which might leave /blah/bin in the PATH at some position not the end, or

     PATH=${PATH//\/blah\/bin/}
     PATH=${PATH//::/:}:/blah/bin

which guarantees the location of /blah/bin at the end of the PATH.

Most apps need a .+_HOME variable, so the above will become:

     export BLAH_HOME=/usr/local/blah
     PATH=${PATH//${BLAH_HOME}\/bin/}
     PATH=${PATH//::/:}:${BLAH_HOME}/bin




More information about the fedora-list mailing list