从 shell 脚本设置环境变量 [英] Set Environment Variable from shell script

查看:72
本文介绍了从 shell 脚本设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆