Scope of "setenv" in csh Versus "export" in bash
Asked Answered
C

1

10

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!

Cornfield answered 27/6, 2013 at 18:50 Comment(4)
what do you mean by "then I go back to csh"? What exactly do you type to "go back to csh"?Iso
csh is my login shell so when I'm in bash and type "exit", it goes back to csh.Cornfield
Right. when you export a variable, it is exported to children, but it is not exported to the parent. When you type "exit", all variables disappear.Iso
if you have logged in with csh then the answer is obviously the one rici gave. You said you did this "in the opposite order", which implied you got this unusual result from a csh login going to sh and then bash/sh login going to csh. You should make this clearer in your question.Warplane
I
15

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.

Iso answered 27/6, 2013 at 21:6 Comment(1)
Makes lots of sense! Thank you!Cornfield

© 2022 - 2024 — McMap. All rights reserved.