What is the equivalent of pathmunge() in csh?

-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.

clacke

Posted 2014-11-06T11:50:32.343

Reputation: 218

"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.703

csh 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 :$PATH: and a case 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.

– clacke – 2014-11-06T13:30:06.893

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

Answers

0

The official workaround for missing array management in csh is to use the Modules system (environment-modules package on Debian).

clacke

Posted 2014-11-06T11:50:32.343

Reputation: 218

0

I finally went with the solution of shelling into bash, because it doesn't seem possible to solve this within the ''csh'' language.

~/bin/munge_echo:

#!/bin/bash

function munge() {
  case ":$1:" in
    *:$2:*) echo "$1" ;;
    ::) echo "$2" ;;
    *) echo "$2:$1" ;;
  esac
}

munged="$1"

shift

while [ -n "$1" ]; do
  munged="$(munge "$munged" "$1")"
  shift
done

echo "$munged"

~/.csh:

setenv PATH `~/bin/munge_echo "$PATH" ~/.local/bin ~/bin`

clacke

Posted 2014-11-06T11:50:32.343

Reputation: 218

Wouldn't be the first time. – user1686 – 2014-11-25T13:04:59.017

1Yeah, but that's also why it's so difficult to find information on csh on the internet. Everything routes to "considered harmful" or "top 10 reasons not to use" or "consider not using csh". :-) – clacke – 2014-11-26T14:01:07.810