-1
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.
"Consider not using csh" is an automatic -1 ... – clacke – 2014-11-06T11:52:48.577
1If you
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). – AFH – 2014-11-06T12:02:41.827@clacke putting "example is automatic -1" in the comments is not constructive. The question states you'd like to use
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. – Raystafarian – 2014-11-06T13:16:27.703csh questions everywhere on the internet tend to get an automatic "consider using bash", indeed I found one of the near the top to a question similar to this one. just pre-empting some of the noise. – clacke – 2014-11-06T13:25:12.733
Grepping can easily give false positives, also I would like to avoid going into a sub-process if possible. The cleanest bash solution uses
– clacke – 2014-11-06T13:30:06.893:$PATH:
and acase
statement that makes*:$1:*
match a no-op, to avoid shelling out. Since csh supports arrays I'm hoping there are actual array operators, unfortunately the man page and http://tcsh.org/ wiki are silent on "array", "list" or "sequence", except that they say that this or that special variable is an array.If I'm shelling out anyway, maybe the easiest way is to just do the processing in bash. :-) – clacke – 2014-11-06T13:30:29.910
Ok, according to http://grymoire.com/Unix/CshTop10.txt csh doesn't even support functions, so maybe I'm way out on a limb here already.
– clacke – 2014-11-06T13:40:27.123