Scope of "setenv" in csh Versus "export" in bash

Dao Lam picture Dao Lam · Jun 27, 2013 · Viewed 34.8k times · Source

From what I've read, setenv in csh and export in bash are equivalent. However, I found some strange issues while working with the two.

When I open putty and start typing:

setenv TEMP yes
echo $TEMP  //this give me "yes"

then I go to bash and type

echo $TEMP //this give me "yes" as well

However, if I do it in the opposite order, it wouldn't give the same results. Specifically, when I go to bash first and type

export TEMP=no
echo $TEMP //this give me "no"

then I go back to csh and type

echo $TEMP // this give me "Undefined Variable"

Shouldn't it give me "no" as well? Am I missing something?

Thank you!

Answer

rici picture rici · Jun 27, 2013

Exporting a variable means that a copy of that variable is placed into the environment of any newly created child processes. It is a copy of the variable; if the child process modifies the variable, the parent does not see the modification. Moreover, if a child exports a variable, it does not become visible in the parent.

Hence, your two cases are asymmetrical. When you start in csh, export a variable, and then start bash, bash sees the exported variable. When you then export a new variable in bash and exit from bash to go back to csh, all of the variables created in the bash session disappear.

If you were to export a variable in bash and then start up a child csh (by typing csh), you would almost certainly see the exported variable.