# Sometimes it can be annoying to try setting the PATH environment variable # correctly on various computers. In particular, one can end up with # multiple or duplicate entries in PATH. I've found the following shell # functions useful in setting PATH while avoiding duplication. Below is a # section out of my .bash_profile file that defines the functions # "append2list" and "prepend2list", followed by sample usages. # # --Greg Hammett, Princeton Plasma Physics Lab, April 23, 2005 # ####################################################################### # append2list $1 $2 # will append $1 to list $2 if $1 is not already contained in $1 # also insures that directory $1 exists # useful for avoiding duplicate entries in PATH or MANPATH, etc. # # (note: this could be confused by partial matches, but that is probably # rare) # function append2list() { if test -d $1 ; then match=`echo ${!2} | grep $1` if [ "$match" == "" ]; then eval ${2}=${!2}:$1 ; fi unset match fi } function prepend2list() { if test -d $1 ; then match=`echo ${!2} | grep $1` if [ "$match" == "" ]; then eval ${2}=$1:${!2} ; fi unset match fi } # # Note: ${!2} is indirection, it is like ${$2}. # # add directories to PATH: prepend2list "$HOME/bin" PATH append2list /opt/OpenOffice/program PATH