2

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/path
/some/other/path

# Some comments
/foo/path

# Another group
/bar/path

$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?

3 Answers 3

2

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 )
end

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.

2
  • Thanks! Would that support empty lines too? Commented Mar 12, 2013 at 0:48
  • @user815423426: Only if they have backslashes. I'll update my answer to demonstrate. Commented Mar 12, 2013 at 0:58
2

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 … )
2
  • 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). Commented Mar 11, 2013 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. Commented Mar 12, 2013 at 18:02
1

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

You must log in to answer this question.

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