csh setenv problem

Ivan Warner,G44 SSTF,6632, iwarner at zaphod.axion.bt.co.uk
Sat Aug 19 00:04:13 AEST 1989


>From article <SAAF.89Aug16153940 at joker.optics.rochester.edu>, by saaf at joker.optics.rochester.edu (Lenny Saaf):
> I want to execute a csh script and set some variables for use in the
> parent shell.  As I understand it, the script in the file is executed
> in a subshell, so I think I have to "export" any csh variables I set
> in the shell script.  That is, use setenv as opposed to set.  Well, I
> can't seem to get it to work.  The shell script file looks like this:
> 
> # csh script
> # filename is testscript
> setenv FOO 'bar'
> echo $FOO
> # end
> 
> The result:
> 
> [1]% chmod +x testscript
> [2]% testscript
> bar
> [3]% echo $FOO
> FOO: Undefined variable.
 
	Yes, the script is run in a sub-shell.

	But, anything set up using setenv is not passed back to the parent
shell either. As soon as the child shell exits, all environment variables are
thrown away. The environment variables are, however, passed down from parent
to child (set variables are not).

	The Bourne Shell (sh) has an export command, which exports
a variable to the parent shell. Csh does not have this.

	You have to 'source' the script file, i.e.

	source filename

	instead of just 'filename'. This runs the script in the current
shell (without spawning a sub-shell). Any setenvs/set/alias commands will
be in effect when this is done. You cannot have parameters to the script
file when using 'source' though (I think).


	Ivan Warner



More information about the Comp.unix.questions mailing list