shell加载配置文件

shell有不同的启动方式,根据启动方式的不同会加载不同的配置文件,从而配置不同的环境变量

我们比较常见的启动方式有:

1.通过linux控制台登录或者ssh方式,启动shell,这种情况为登录式启动shell

 会依次加载/etc/environment, /etc/profile, ~/.bash_profile (或者~/.bash_login 或者 ~/.profile)

而~/.bash_profile中又会加载~/.bashrc,而~/.bashrc又加载  /etc/bashrc ,  /etc/bashrc又加载  /etc/profile.d/*.sh

2. 通过bash -l 登录式启动shell,会加载 /etc/profile, ~/.bash_profile 但不会加载/etc/environment

3,通过bash 非登录式启动shell,会加载~/.bashrc,前面说过~/.bashrc脚本又会加载/etc/bashrc ,  /etc/bashrc又加载  /etc/profile.d/*.sh

可以看出上面的三种方式,不管时登陆时还是非登陆时,都会加载~/.bashrc, /etc/bashrc ,  /etc/profile.d/*.sh中的内容

而 /etc/profile, ~/.bash_profile只有在登录式启动的时候才会加载

如果想给所有的用户登录式使用某个环境变量,就在/etc/profile中添加,如果只是给某个用户登录时使用某个变量,就在用户专属文件 ~/.bash_profile中添加

还有一种启动方式时定时任务的cron启动方式

cron启动方式只会加载/etc/environment,而不会加载/etc/profile或者 /etc/bashrc

在配置文件中输出配置文件名。测试结果如下

Last login: Fri Feb  7 17:40:49 2020 from 172.25.10.1  //ssh登录时启动新进程,
/etc/environment*****in /etc/profile     //此处能输出,说明加载了/etc/environment文件
/etc/profile                             //此处说明加载了 /etc/profile
/etc/profile*****in vagrant/.bash_profile //此处说明加载了 ~/.bash_profile
vagrant/.bash_profile*********in vagrant/.bashrc //此处说明加载了 ~/.bashrc
vagrant/.bashrc*****in /etc/bashrc
/etc/bashrc                              //此处说明加载了 /etc/bashrc
[vagrant@localhost ~]$ sudo su           //sudo su命令 废弃原有的环境变量,并重新加载配置文件,好像开启了新的进程(但是$$又没有改变)
/etc/environment******in root/.bashrc   //此处输出说明加载了/etc/environment/和~/.bashrc
root/.bashrc
root/.bashrc*****in /etc/bashrc         //此处说明加载了 /etc/bashrc
/etc/bashrc
[root@localhost vagrant]# bash         //bash非登录式启动新进程
/etc/bashrc******in root/.bashrc       //此处说明加载了~/.bashrc
root/.bashrc
root/.bashrc*****in /etc/bashrc        //此处说明加载了/etc/bashrc
/etc/bashrc
[root@localhost vagrant]# bash -l      //bash登录式启动新进程
/etc/bashrc*****in /etc/profile        //加载了/etc/profile
/etc/profile
root/.bash_profile                      //加载了~/.bash_profile
root/.bash_profile******in root/.bashrc
root/.bashrc                            //加载了~/.bashrc
root/.bashrc*****in /etc/bashrc        //加载了 /etc/bashrc
/etc/bashrc
[root@localhost vagrant]# (echo a)
a

由以上测试可以看出

ssh登录式进程加载了/etc/environment,/etc/profile,~/.bash_profile(~/.bash_profile内部又加载了~/.bashrc, ~/.bashrc内部又加载了/etc/bashrc)

sudo su切换到root用户,加载了/etc/environment/和~/.bashrc(其中~/.bashrc内部又加载了/etc/bashrc)

bash命令创建非登录式新进程加载了 ~/.bashrc(其中~/.bashrc内部又加载了/etc/bashrc)

bash -l命令创建登录式新进程 加载了/etc/profile,~/.bash_profile(~/.bash_profile内部又加载了~/.bashrc, ~/.bashrc内部又加载了/etc/bashrc)


总结:登录式会加载 /etc/profile,~/.bash_profile ,初期用户名密码登录或者ssh登录,在加载前面两个之前,还会加载/etc/environment
非登陆式会加载 ~/.bashrc
sudo命令会清楚环境变量并重新/etc/environment/和~/.bashrc

不管哪种方式,最终都会加载~/.bashrc, /etc/bashrc,  /etc/profile.d/*.sh

cron方式会加载/etc/environment, 不会加载/etc/profile,至于会不会加载/etc/bashrc还有待验证

/etc/environment的加载时机为①ssh登陆时②cron启动时③sudo su时

猜你喜欢

转载自www.cnblogs.com/gaoBlog/p/12273852.html