0

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.

7
  • "Consider not using csh" is an automatic -1 ...
    – clacke
    Nov 6, 2014 at 11:52
  • 1
    If 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
    Nov 6, 2014 at 12:02
  • @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. Nov 6, 2014 at 13:16
  • 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
    Nov 6, 2014 at 13:25
  • 1
    Ok, according to grymoire.com/Unix/CshTop10.txt csh doesn't even support functions, so maybe I'm way out on a limb here already.
    – clacke
    Nov 6, 2014 at 13:40

2 Answers 2

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`
2
  • Wouldn't be the first time. Nov 25, 2014 at 13:04
  • 1
    Yeah, 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
    Nov 26, 2014 at 14:01
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .