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