8

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!

G. Cito
  • 6,210
  • 3
  • 29
  • 42
Dao Lam
  • 2,837
  • 11
  • 38
  • 44
  • 1
    what do you mean by "then I go back to csh"? What exactly do you type to "go back to csh"? – rici Jun 27 '13 at 20:07
  • csh is my login shell so when I'm in bash and type "exit", it goes back to csh. – Dao Lam Jun 27 '13 at 20:32
  • 4
    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. – rici Jun 27 '13 at 21:04
  • 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. – G. Cito Jun 28 '13 at 02:34

1 Answers1

14

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.

rici
  • 234,347
  • 28
  • 237
  • 341