Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Inspired by the accepted answer to this thread, I am trying to replicate a similar construction in tcsh (version 6.14.00) with no luck:

setenv new_PATH (

# Some comments

# Another group

$PATH) # Attach $PATH at the end in case we had previous declarations

setenv PATH `sed -e '/^#/'d -e '/^$/'d << EOF | paste -d ":" -s $new_PATH`

I get the error "Too many ('s" . What am I doing wrong?

share|improve this question
up vote 2 down vote accepted

You can do this:

set path = (        \
    /this/dir       \
# This is a comment \
    /that/dir       \
    /another/dir    \

Another approach is to read the directory names from a file:

set path = ()
foreach dir (`/bin/cat path.txt`)
    set path = ( $path $dir )

Note that I needed to specify /bin/cat since I just clobbered my $PATH.

You can replace cat with something that filters out comments (such as a simple sed -- or rather /bin/sed -- command).

This will have problems with directory paths that have spaces or other funny characters, but you should try to avoid those anyway.

share|improve this answer
Thanks! Would that support empty lines too? – Amelio Vazquez-Reina Mar 12 '13 at 0:48
@user815423426: Only if they have backslashes. I'll update my answer to demonstrate. – Keith Thompson Mar 12 '13 at 0:58

setenv sets environment variables, and they can't be arrays. setenv has the syntax of an ordinary command.

The set command can set a shell variable to be an array. You need to put all the elements on the same line.

set new_PATH = ( /some/path /some/other/path … )

Tcsh synchronizes the path shell variable with the PATH environment variable, so you don't need to do that manually.

set new_PATH = ( /some/path /some/other/path … )
set new_PATH = ( $new_PATH /more )
set path = $new_PATH

or directly

set path = ( /some/path /some/other/path … )
share|improve this answer
Thanks Gilles. I guess then that there is no (easy) way then to have a multi-line syntax in tcsh similar to the one I described in the OP (with only one path per line and maybe comments and spaces). – Amelio Vazquez-Reina Mar 11 '13 at 23:37
@user815423426 I don't know of a convenient syntax that way, but I'm no tcsh expert. Tcsh hasn't been maintained in over a decade, you can't expect to find as many convenient features as in bash and zsh. – Gilles Mar 12 '13 at 18:02

tcsh defines's the environment variable for PATH like so: setenv PATH /path

and adds to the variable like so: set PATH ($PATH /add/path/here /another/here)

If you pass the environment variable into itself then you can just add onto it form where it is currently set to.

From looking at how easily environment variables are set. There is no reason for a script. It's one command to set the variable.

NOTE to the OP: You're last line setting the variable should set set PATH ($PATH <then issue sed>).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.