一、概述
常用的环境变量配置文件主要是:
/etc/profile /etc/bashrc ~/.bashrc ~/.bash_profile
测试一下环境变量的优先级,和后台运行情况下变量的优先级
分别进行设置
/etc/profile export aaa=1
/etc/bashrc export aaa=2
~/.bashrc export aaa=3
~/.bash_profile export aaa=4
二、试验
1、前台直连测试
#打开新窗口,直接测试
echo $aaa ==>4
#注释掉~/.bash_profile export aaa=4 ,打开新窗口
echo $aaa ==>3
#注释掉~/.bashrc export aaa=3 ,打开新窗口
echo $aaa ==>2
可以看出前台直接连服务器,环境变量的优先级
~/.bash_profile > ~/.bashrc > /etc/bashrc > /etc/profile
2、后台运行(ansible获取)
另一台机器配置好ansible
ansible 192.168.10.131 -m shell -a "echo \$aaa" -i hosts
输出为3
将~/.bashrc export aaa=3注释掉
ansible 192.168.10.131 -m shell -a "echo \$aaa" -i hosts
输出为2
将/etc/bashrc export aaa=2注释掉
ansible 192.168.10.131 -m shell -a "echo \$aaa" -i hosts
输出为空
当使用shell定时脚本或者ansible时
环境变量优先级
~/.bashrc > /etc/bashrc 另外两个环境变量直接不生效