我正在从脚本 A 调用 bash 脚本 B。在脚本 A(父脚本)中,我正在导出一些变量。我想在脚本 B(下标)中使用这些变量,但是变量值没有从脚本 A 传递到脚本 B。有没有办法从脚本 B 访问变量值?
#!/bin/bash
# script_A.sh
export VAR="value"
enter code here
sudo -u user ./script_B.sh
#!/bin/bash
# script_B.sh
echo $VAR # this prints nothing
最佳答案
正如@geekosaur 提到的,sudo
出于安全原因重置环境。
要保护环境,请通过 -E
切换到 sudo
。
来自 sudo
联机帮助页:
-E
The
-E
(preserve environment) option indicates to the security policy that the user wishes to preserve their existing environment variables. The security policy may return an error if the-E
option is specified and the user does not have permission to preserve the environment.
关于bash - 通过 sudo 从另一个 shell 脚本调用 shell 脚本 - 环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10098414/