0

I am writing a simple script to source path variable as below:

#!/bin/csh
echo "setting path"
set path=($path /sbin:/mscc/apps)

However, the path variable does not change and remain the same. The csh shell does not have any errors/warnings.

If I add an invalid line, for example:

setenv PATH $PATH:sbin

It complains "Bad substitute." but the two path above are added. Why is it happening?

5
  • 2
    Are you executing the sample script and expecting your current (the parent) shell's $path to change?
    – Jeff Schaller
    Commented Mar 16, 2018 at 17:16
  • Yes, I expect my current opened shell (from GNOME) to change the $path. I tried your answers with both ways and it is not working. The only time it works is when I add an invalid line at the end of the file (eg, like dummy text: ppp).
    – Allen W
    Commented Mar 16, 2018 at 17:36
  • sourcing the script doesn't change the path? Did you try using a space instead of a colon?
    – Jeff Schaller
    Commented Mar 16, 2018 at 17:38
  • Yes, to eliminate the concern, I am setting only one path variable now which has the same result.
    – Allen W
    Commented Mar 16, 2018 at 17:46
  • Can you edit a transcript of your session into your question, showing your initial path, your execution of the script, and the resulting path?
    – Jeff Schaller
    Commented Mar 16, 2018 at 18:40

3 Answers 3

1

I can only duplicate this if I execute such a script:

$ echo $path
/usr/local/bin /usr/bin /usr/local/sbin /usr/sbin

$ ./script.csh
setting path
$ echo $path
/usr/local/bin /usr/bin /usr/local/sbin /usr/sbin

If you instead source the script, you'll see different behavior:

$ source script.csh
setting path
$ echo $path
/usr/local/bin /usr/bin /usr/local/sbin /usr/sbin /sbin:/mscc/apps

(Perhaps you want /sbin /mscc/apps with a space instead of a colon separating the elements?)

1

It is not the same to run a script and to source a script.  When you run a script (with ./script.sh) you are spawning a new process, which will execute the script. In the meantime, the shell process (the one where you wrote ./script.sh) will wait until that other process finishes, and maybe capture the output.

The spawned process is able to modify its own environment (for example, the path variable) but not the one of his parent. When it finishes, that process will die, and the parent will keep having the same value in path.

What you want is the parent process to run set path=($path /sbin:/mscc/apps). To do so, write source ./script.sh instead of ./script.sh. In this case the line #!/bin/csh is not needed, since it is just a comment.

0

Both of these worked for me in csh on HPUX:

Version 1 (update $path):

#!/bin/csh
echo "setting path"
set path=($path:q /sbin /mscc/apps)
echo '==[ echo $PATH - updated/exported by csh ]=='
echo $PATH
echo '==[ echo $path ]=='
echo $path
echo '==[ foreach pdir ($path:q) ; echo $pdir ; end - $path is a list ]=='
foreach pdir ($path:q)
    echo $pdir
end
echo '==[ sub-process $PATH - updated PATH exported by csh automatically ]=='
perl -e 'print $ENV{PATH}."\n";'

Version 2 (update $PATH):

#!/bin/csh
echo "setting path"
setenv PATH "${PATH}:/sbin:/mscc/apps"
echo '==[ echo $PATH - exported by csh ]=='
echo $PATH
echo '==[ echo $path - updated from $PATH by csh ]=='
echo $path
echo '==[ foreach pdir ($path:q) ; echo $pdir ; end - $path is a list ]=='
foreach pdir ($path:q)
    echo $pdir
end
echo '==[ sub-process $PATH - updated PATH exported by csh automatically ]=='
perl -e 'print $ENV{PATH}."\n";'

You must log in to answer this question.

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