How to export an environment variable from .sh file in zsh?
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.
Related videos on Youtube
Comments
-
ams over 1 year
I have the following shell script
TEST=/foopath export TEST
It is in a file called
test.sh
and on which I ranchmod +x test.sh
When I run
./test.sh
I expect that I can then executeecho $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?