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 ,/etc/bashrc又加载  /etc/profile.d/*.sh)

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/bashrc又加载  /etc/profile.d/*.sh)


总结:登录式会加载 /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时

在我们写完定时处理任务的脚本时,

在测试时我们往往式通过在命令行中输入 bash 脚本文件路径的方式来执行,而真正的定时处理是通过cron来处理的。

上面已经提到,两者会加载不同的配置文件。其中命令行bash的方式会加载 ~/.bashrc (~/.bashrc内部又加载了/etc/bashrc, /etc/bashrc又加载  /etc/profile.d/*.sh)

而cron方式只加载了/etc/environment,所以在涉及到使用配置文件的环境变量时,两者会有差异

那么如何去除这种差异呢?

方式1.最简单的方式就是直接在/etc/environment中设置环境变量,这样两中方式都会取到。命令行bash的方式开启的新进程,虽然不会重新加载/etc/environment,但是会拷贝父进程的环境变量,而父进程的环境量最初是来自ssh登录或者sudo su,这两种方式都会加载/etc/environment。所以使用命令行bash也可以取到/etc/environment中的环境变量

需要注意的是,如果新追加了环境变量,需要重新登录或者重新sudo su来重新加载/etc/environment,否则命令行bash取不到新追加的环境变量。这是因为在父进程的环境变量中不存在新追加的环境变量

方式2.另一种方式就是在脚本文件中直接通过source(或者 .)的方式引入环境变量所在的文件,这样不论是命令行bash还是cron方式都会取到环境变量了

如果使用方式2的话,每个脚本文件都需要导入配置文件(当然这也不算麻烦),而方式1并不需要

而方式2的好处是可以自定义将环境变量的文件,并与普通的代码文件一样使用git管理。当然也可以写入/etc/profile这样的文件中,再导入/etc/profile。不过就无法通过git来管理了

原文地址:https://www.cnblogs.com/gaoBlog/p/12273852.html