I have the following shell script

export TEST

It is in a file called test.sh and on which I ran chmod +x test.sh

When I run ./test.sh I expect that I can then execute echo $TEST and see the output /foopath but I see nothing.

What changes are needed to make the above script export the variable $TEST when I run ./test.sh. Is this a bash vs. zsh difference?

marked as duplicate by Jeff Schaller, Rui F Ribeiro, countermode, roaima, GAD3R Nov 27 '16 at 21:37

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Assigned variables exports to daughter processes but to parent one. If you like to use vars from script you should use source command. source test.sh – Costas Nov 27 '16 at 18:06
  • There may be a better duplicate (in fact, I'm pretty sure there has been a question specifically about environment variables, but I couldn't find it). The accepted answer addresses this case exactly though. – Michael Homer Nov 27 '16 at 18:53

It's a parent- v. child-shell difference.

When you run test.sh, a new shell is started to run it; the variable is exported within that shell. That means the new shell is aware of it, and any of its own children. The parent shell, the one you started test.sh from, isn't affected at all (and can't be).

To see the variable in your current shell, you need to source the script instead:

. test.sh

(with a space between . and test.sh). This will run test.sh without starting a new shell.

Not the answer you're looking for? Browse other questions tagged or ask your own question.