Bash 从shell脚本设置环境变量

Bash 从shell脚本设置环境变量,bash,shell,unix,Bash,Shell,Unix,我有一个环境变量,比如 ABC_XYZ=false 当我echo$ABC_XYZ 它给了我false 现在,我在一个脚本中执行以下步骤 >echo $ABC_XYZ false >ABC_XYZ=true >echo $ABC_XYZ true 现在退出脚本之后 echo$ABC_XYZ再次给我false 我可以通过任何方式从脚本内部将值设置为true 提前感谢。您必须使用源代码在shell的当前上下文中运行脚本 假设脚本位于当前目录中: $ source ./scrip

我有一个环境变量,比如

ABC_XYZ=false
当我
echo$ABC_XYZ
它给了我
false
现在,我在一个脚本中执行以下步骤

>echo $ABC_XYZ 
false
>ABC_XYZ=true
>echo $ABC_XYZ
true
现在退出脚本之后
echo$ABC_XYZ
再次给我
false
我可以通过任何方式从脚本内部将值设置为true


提前感谢。

您必须使用
源代码在shell的当前上下文中运行脚本

假设脚本位于当前目录中:

$ source ./script_filename

引用自
帮助源

源:源文件名[参数] 从当前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或任何其他进程)中可用,您需要在脚本中像这样导出环境变量:

export ABC_XYZ

您必须使用
source
在shell的当前上下文中运行脚本

假设脚本位于当前目录中:

$ source ./script_filename

引用自
帮助源

源:源文件名[参数] 从当前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或任何其他进程)中可用,您需要在脚本中像这样导出环境变量:

export ABC_XYZ

尝试使用以下命令运行以下脚本文件:

source ./export.bash

尝试使用以下命令运行以下脚本文件:

source ./export.bash

添加的源代码~/.bash_配置文件和导出ABC_XYZ仍然没有成功
源代码脚本
必须在命令行中编写,而不是在脚本中添加的源代码~/.bash_配置文件和导出ABC_XYZ仍然没有成功
源代码脚本
必须在命令行中编写,而不是在脚本中这并不能回答问题问题一旦你有足够的钱,你将能够;相反好的,谢谢你的建议。下次我会做的。我是个新手,不会堆栈溢出。这并不能回答这个问题。一旦你有足够的钱,你将能够;相反好的,谢谢你的建议。下次我会做的。我太新了,不会堆积溢出物。