[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: safe way to set a path



* Wichert Akkerman (wichert@wiggy.net) wrote:
> Previously Michael A. Miller wrote:
> > # A function to add to PATH.  If it is already there, don't bother...
> > add_to_path () {
> >     if [ -d $1 ]; then 
> > 	if [ ! `echo $PATH | grep $1` ]; then
> 
> That is wrong, it fails if what you want to add is a subset of an
> existing path. For example considering PATH includes /usr/local/usr/bin and
> you want to add /usr/bin.


	I use the following code to add to the values of my PATH variables.


addtopath() {
  local __command__
  local __value__
  local __item__
  local __is_new__
  
  __is_new__=1
  __command__="echo `echo '$'$1`" 
  __value__=`eval $__command__`
  for __item__ in `echo $__value__|sed 's/:/ /g'`
  do
    if [ "$__item__" = "$2" ]; then
	__is_new__=0
    fi
  done
  if [ "$__is_new__" -eq "1" ]; then
    export $1=$2:$__value__
  fi
}


	This function receives two arguments: the name of the variable and the
	value to be added. So I can use it like this:

addtopath PATH /usr/local/foo/bin
addtopath MANPATH /usr/local/foo/man
addtopath LD_LIBRARY_PATH /usr/local/foo/lib



Reply to: