[英]export doesn't work in shell script but does via CLI
I have the following file - test.sh - in .: 我在以下文件-test.sh-中:
#!/bin/sh
export ASDF=test
I do chmod +x test.sh
,then ./test.sh
and finally echo $ASDF
and... nothing. 我先执行chmod +x test.sh
,然后执行./test.sh
,最后echo $ASDF
./test.sh
和...什么都没有。 It's as though $ASDF hasn't been set. 好像没有设置$ ASDF。 But if I do it via the CLI instead of a shell script it works just fine and $ASDF is defined. 但是,如果我通过CLI而不是Shell脚本来执行此操作,则可以正常工作,并且已定义$ ASDF。
Why isn't the shell script working? Shell脚本为什么不起作用?
It is because: 这是因为:
./test.sh
will create a sub shell and set env variables in the sub shell. 将创建一个子外壳,并在子外壳中设置env变量。 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
:
. ./test.sh
OR: 要么:
source ./test.sh
Now that variable ASDF
will be available in current shell also. 现在,变量ASDF
也将在当前Shell中可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.