Has this ever happened to you? (i bet it has):
$ export PATH=$PAHT:/usr/local/foo/bin
$ ls
ls: command not found
It is so easy to have a typo in that PATH assignment, and so hard to recover from it (esp. when not using X11).
Other things that come to mind with $PATH and similar variables (like $LD_LIBRARY_PATH or $CLASSPATH):
- there may be multiple entries for the same part
- removing an entry from $PATH requires manual interaction
- '.' may end up in the middle of the path
The 'path' binary created by David Ingalls Bell solves all these issues, as can be seen from its project description on freshmeat.net:
path can be used to manipulate a colon-separated list
of paths contained in an environment variable such as
PATH. New paths can be added either to the beginning or
the end of the path list, paths can be removed from the
path list, and several checks can be made on the paths
contained in the path list. All duplicate paths in the
list are removed.
( http://freshmeat.net/projects/path/ )
There is only one issue left: 'path' can modify existing $PATH variable contents just fine but it can't modify the environment of your current shell.
So i have created a set of bash functions wrapping 'path' a long time ago that i now want to share. The functions i created are named 'pathadd', 'pathfront', 'pathdel' etc.
Lets have a look at one of these first:
# prepend directory to path if not already present
# "pathadd dir" -> prepend to $PATH
# "pathadd MY_PATH dir" -> prepend to $MY_PATH
function pathfront()
{
if [ $# = 2 ]
then
export $1=`path -var $1 -rb $2`;
else
export PATH=`path -var PATH -rb $1`;
fi
}
'pathfront' will add a directory to the beginning of $PATH or another specified variable. If the directory was already included in the path it is moved to the front, no duplicates are created. If '.' is part of the path it will always be the first ement of the path, the given directory wil becomenumber 2 in the list then.
Examples:
# if called with one argument the variable defaults to $PATH
pathadd /usr/local/mysql/bin
# when called with two arguments the first is a varaible name
pathadd LD_LIBRARY_PATH /usr/local/mysql/lib/mysql
So if i want to test things with a specific mysql version installed on my box i do this first:
pathfront /usr/local/mysql-x.y.z/bin
pathfront /usr/local/mysql-x.y.z/libexec
pathfront LD_LIBRARY /usr/local/mysql-x.y.z/lib/mysql
and after i'm done i'll run the reverse to clean up the pathes:
pathdel /usr/local/mysql-x.y.z/bin
pathdel /usr/local/mysql-x.y.z/libexec
pathdel LD_LIBRARY /usr/local/mysql-x.y.z/lib/mysql
A full list of bash wrapper functions for path incldues:
- path_add
- add entry to end of path if it doesn't already exists, else leave path as is
- path_back
- add entry to end of path, if it already exits then move it to the end instead
- path_front
- add entry to front of part or move it there if it already exists, but make sure '.' is always the first element if it is part of the path
- path_del
- remove entry from the path
- path_list
- show current path elements line by line
- path_clean
- clean up duplicate entries and remove entries pointing to nonexisting locations
- pach_check
- check current path and report possible problems
References: