Linux Shell 环境变量 /etc/profile /etc/bashrc ~/.profile ~/.bashrc 作用时机

1.为了一探环境变量起作用的时机,我们在各个关键文件中追加echo
1-1.在/etc/profile中追加`
echo "sourcing /etc/profile..."
1-2.在/etc/bashrc中追加
echo "sourcing /etc/bashrc..."

or

在/etc/bash.bashrc (Debian/Ubuntu)中追加:
echo "sourcing /etc/bash.bashrc..."
1-3.在~/.profile中追加
echo "sourcing ~/.profile..."
1-4.在~/.bashrc中追加
echo "sourcing ~/.bashrc..."
2.在几种操作时,查看效果

操作1,打开terminal
操作2,登陆用户,执行 su - user
操作3,登陆root用户,执行 su - root
操作4,常用提权命令,执行 sudo su

3. 结果
3-1. 操作1,打开terminal:

sourcing /etc/bash.bashrc…
sourcing ~/.bashrc…

3-2. 操作2,su - user

sourcing /etc/bash.bashrc…
sourcing /etc/profile…
sourcing ~/.bashrc…
sourcing ~/.profile…

3-3. 操作3, su - root

sourcing /etc/bash.bashrc…
sourcing /etc/profile…

3-4. 操作4,sudo su

sourcing /etc/bash.bashrc…

4. 总结

打开terminal的时候:
bash变量起了作用,且与全局/本地无关,只与bash有关。

su - user 登陆用户的时候:
全局和本地的4个文件,都起了作用。

su - root 登陆root的时候:
全局的 profile 和 bashrc 起了作用。

sudo su进入了未登陆root:
只有全局bashrc起了作用。

参考文献:
https://www.linuxprobe.com/diff-bashrcprofile.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值