简体   繁体   English

Bash 脚本使用 if function 导出变量

[英]Bash script exporting variables with if function

I got some not working script about checking if the variable is empty, and based on this create the next variable and export it.我得到了一些关于检查变量是否为空的无效脚本,并基于此创建下一个变量并将其导出。

export VARIABLE=macmac123

if [ -z "$VARIBLE" ]
then 
      unset $VAR2 
else 
      export VAR2="test"
fi

Next I run this script./script.sh and then running echo $VAR2 doesn't show anything.接下来我运行这个脚本。/script.sh 然后运行 echo $VAR2 没有显示任何内容。 Any ideas?有任何想法吗? I can't change the whole script, because I need this, but it doesn't export anything into environmental variables.我无法更改整个脚本,因为我需要这个,但它不会将任何内容导出到环境变量中。

Shell scripts are executed in a subshell. Shell 脚本在子 shell 中执行。 If you want the variables in the current shell you have to source the script like: source script.sh or . script.sh如果你想要当前 shell 中的变量,你必须像这样获取脚本: source source script.sh. script.sh . script.sh (dot is an alias for source ). . script.sh (点是source的别名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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