The snippet below nicely handles this problem. It has been tested on Solaris and Linux.
AddPath () # Add argument to $PATH if: # - it is not already present # - it is a directory # - we have execute permission on it # # This snippet is public domain; you may use it freely. Death to copyright, patents, # and all other forms of intellectual monopoly. # { _folder=$1 echo " $PATH " | sed 's/:/ /g' | grep " $_folder " > /dev/null [ $? -ne 0 ] && [ -d $_folder ] && [ -x $_folder ] && PATH=$PATH:$_folder export PATH } # Add some common paths: AddPath /usr/bin AddPath /usr/local/bin AddPath /opt/somepackage/bin