How to export an environment variable from .sh file in zsh?

8,097

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.

Share:
8,097

Related videos on Youtube

ams
Author by

ams

I love software development.

Updated on September 18, 2022

Comments

  • ams
    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 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?