In bash, a common function implemented in e.g. /etc/bashrc is pathmunge()
, which appends or prepends a path to PATH
if it isn't already listed among the elements in PATH
.
Example:
$ echo $PATH
/bin:/usr/bin
$ pathmunge /home/me/bin
$ echo $PATH
/home/me/bin:/bin:/usr/bin
$ pathmunge /home/me/bin
$ echo $PATH
/home/me/bin:/bin:/usr/bin
The naive way to add a path would be as follow:
$ echo $PATH
/bin:/usr/bin
$ PATH=/home/me/bin:$PATH
$ echo $PATH
/home/me/bin:/bin:/usr/bin
$ PATH=/home/me/bin:$PATH
$ echo $PATH
/home/me/bin:/home/me/bin:/bin:/usr/bin
I know very little about csh, and I just want to do this simple thing: add a path to a list if it's not already there. I am aware that csh uses path
rather than PATH
and that is is a proper csh array.
Maybe csh has some array function like if !(foobar in $path) then blah
that makes a function like pathmunge()
unnecessary. In that case, that is the correct answer.
echo $PATH | grep /home/me/bin
then you can check the return code and add the new path only if it non-zero (ie string not found).csh
, if someone asks what your purpose is (if it's not outlined in the question), you may get answers that are better than using that command. It doesn't make them less valid, but you can indicate you're looking to use that specific command.