简体   繁体   中英

export doesn't work in shell script but does via CLI

I have the following file - test.sh - in .:

#!/bin/sh
export ASDF=test

I do chmod +x test.sh ,then ./test.sh and finally echo $ASDF and... nothing. It's as though $ASDF hasn't been set. But if I do it via the CLI instead of a shell script it works just fine and $ASDF is defined.

Why isn't the shell script working?

It is because:

./test.sh

will create a sub shell and set env variables in the sub shell. Once sub shell exits this variable isn't available in parent shell.

Use this form to avoid forking a sub shell and execute test.sh in the current shell itself:

. ./test.sh

OR:

source ./test.sh

Now that variable ASDF will be available in current shell also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM