Operating System - HP-UX
1760078 Members
2412 Online
108889 Solutions
New Discussion юеВ

Environment variable changes in a csh script

 
TD Clark
Advisor

Environment variable changes in a csh script

We have database environment variables set as part of our .cshrc for a login. When that same person changes his environment variables and runs a csh script the csh script reverts his changed environment variables back to the same that .cshrc sets. Any ideas on this one? Thanks all!
3 REPLIES 3
Dave La Mar
Honored Contributor

Re: Environment variable changes in a csh script

Unless they are set in the script, when you invoke another shell it will use .cshrc.

My .02. Been burned by this.

Best of luck.
dl
"I'm not dumb. I just have a command of thoroughly useless information."
James R. Ferguson
Acclaimed Contributor

Re: Environment variable changes in a csh script

Hi Todd:

You need to set and export the environmental variables you want visible:

# setenv MYTHING myvalue

Regards!

...JRF...
David Lodge
Trusted Contributor

Re: Environment variable changes in a csh script

The problem here is called inheritance. A process may inherit its environment from its parent but can pass nothing back.

This is why you have the differences between running a shell and sourcing a shell.

When you 'run' a shell script, a new copy of the shell is spawned and the script is run in this new spawned - ie no effect to the environment of the current shell.

When you 'source' a shell script, it is loaded and executed in the current shell - therfore it will effect the current environment.

For example (using /usr/bin/sh - csh is evil and shouldn't be used :-)

$ cat env.sh
#!/usr/bin/sh
FRED="a value"
$ FRED=""
$ ./env.sh
$ echo ${FRED}

$ . ./env.sh
$ echo ${FRED}
a value
$

It's similar in csh - except you can use the keyword 'source' instead of a . eg:
% source ./env.sh
% echo $FRED
a value
%

HTH

dave