从 shell 脚本设置环境变量 [英] Set Environment Variable from shell script
问题描述
我有一个像
ABC_XYZ=false
当我 echo $ABC_XYZ
它给了我 false
现在我在一个脚本中,我执行这些步骤
When I echo $ABC_XYZ
It gives me false
Now I am inside a script I do these steps
>echo $ABC_XYZ
false
>ABC_XYZ=true
>echo $ABC_XYZ
true
现在退出脚本后echo $ABC_XYZ
再次给我 false
我可以从脚本内部将值设置为 true 的任何方式.
Now after exiting from the script
echo $ABC_XYZ
again gives me false
Any way through which i can set the value as true from inside the script.
提前致谢.
推荐答案
您必须使用 source
在 shell 的当前上下文中运行脚本.
You will have to run the script in the shell's current context using source
.
假设您的脚本在当前目录中:
Assuming your script is in your current directory:
$ source ./script_filename
或
$ . ./script_filename
引用帮助源
:
source: 源文件名 [参数]从当前 shell 中的文件执行命令.
source: source filename [arguments] Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
<小时>
要使变量在后续分叉的子进程(shell 或任何其他进程)中可用,您需要在脚本中像这样导出环境变量:
To make the variable available in subsequently forked child processes(either shell or any other process), you'll need to export the environment variable like this in the script:
export ABC_XYZ
这篇关于从 shell 脚本设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!