加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 通过sudo – 环境变量从另一个shell脚本调用shell脚本?

发布时间:2020-12-15 22:21:54 所属栏目:安全 来源:网络整理
导读:我正在从脚本A调用bash脚本B.在脚本A(父脚本)中,我正在导出一些变量.我想在脚本B(下标)中使用这些变量,但是变量值没有从脚本A传递到脚本B.有没有办法从脚本B访问变量值? #!/bin/bash# script_A.shexport VAR="value"enter code heresudo -u user ./script_B
我正在从脚本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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读