Bash启动时加载配置文件过程(2)

无论是否交互、是否登录,bash总要配置其运行环境。bash环境配置主要通过加载bash环境配置文件来完成。但是否交互、是否登录将会影响加载哪些配置文件,除了交互、登录属性,有些特殊的属性也会影响读取配置文件的方法。

bash环境配置文件主要有/etc/profile、~/.bash_profile、~/.bashrc、/etc/bashrc和/etc/profile.d/*.sh,为了测试各种情形读取哪些配置文件,先分别向这几个配置文件中写入几个echo语句,用以判断该配置文件是否在启动bash时被读取加载了。

echo "echo '/etc/profile goes'" >>/etc/_profile echo "echo '~/.bash_profile goes'" >>~/.bash_profile echo "echo '~/.bashrc goes'" >>~/.bashrc echo "echo '/etc/bashrc goes'" >>/etc/bashrc echo "echo '/etc/profile.d/test.sh goes'" >>/etc/profile.d/test.sh chmod +x /etc/profile.d/test.sh

①.交互式登录shell或非交互式但带有"--login"(或短选项"-l",例如在shell脚本中指定"#!/bin/bash -l"时)的bash启动时,将先读取/etc/profile,再依次搜索~/.bash_profile、~/.bash_login和~/.profile,并仅加载第一个搜索到且可读的文件。当退出时,将执行~/.bash_logout中的命令。

但要注意,在/etc/profile中有一条加载/etc/profile.d/*.sh的语句,它会使用source加载/etc/profile.d/下所有可执行的sh后缀的脚本。

[root@linuxidc~]# grep -A 8 \*\.sh /etc/profile for i in /etc/profile.d/*.sh ; do if [ -r "$i" ]; then if [ "${-#*i}" != "$-" ]; then . "$i" else . "$i" >/dev/null 2>&1 fi fi done

内层if语句中的【"${-#*i}" != "$-"】表示将"$-"从左向右模式匹配"*i"并将匹配到的内容删除(即进行变量切分),如果"$-"切分后的值不等于"$-",则意味着是交互式shell,于是怎样怎样,否则怎样怎样。

同样的,在~/.bash_profile中也一样有加载~/.bashrc的命令。

[root@linuxidc~]# grep -A 1 \~/\.bashrc ~/.bash_profile if [ -f ~/.bashrc ]; then . ~/.bashrc fi

而~/.bashrc中又有加载/etc/bashrc的命令。

[root@linuxidc~]# grep -A 1 /etc/bashrc ~/.bashrc if [ -f /etc/bashrc ]; then . /etc/bashrc fi

其实/etc/bashrc中还有加载/etc/profile.d/*.sh的语句,但前提是非登录式shell时才会执行。以下是部分语句:

if ! shopt -q login_shell ; then # We're not a login shell ... for i in /etc/profile.d/*.sh; do if [ -r "$i" ]; then if [ "$PS1" ]; then . "$i" else . "$i" >/dev/null 2>&1 fi fi done ... fi

从内层if语句和/etc/profile中对应的判断语句的作用是一致的,只不过判断方式不同,写法不同。

因此,交互式的登录shell加载bash环境配置文件的实际过程如下图:

Bash启动时加载配置文件过程

以下结果验证了结论:

Last login: Mon Aug 14 04:49:29 2017 # 新开终端登录时 /etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes

 

[root@linuxidc~]# ssh localhost # ssh远程登录时 root@localhost's password: Last login: Mon Aug 14 05:05:50 2017 from 172.16.10.1 /etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes

 

[root@linuxidc~]# bash -l # 执行带有"--login"选项的login时 /etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes

[root@linuxidc~]# su - # su带上"--login"/etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes

 

[root@linuxidc~]# vim a.sh # 执行shell脚本时带有"--login"#!/bin/bash -l echo haha [root@linuxidc~]# ./a.sh /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes haha

之所以执行shell脚本时没有显示执行/etc/profile.d/*.sh,是因为它是非交互式的,根据/etc/profile中的【if [ "${-#*i}" != "$-" ]】判断,它将会把/etc/profile.d/*.sh的执行结果重定向到/dev/null中。也就是说,即使是shell脚本(带"--login "选项),它也加载了所有bash环境配置文件。

②.交互式非登录shell的bash启动时,将读取~/.bashrc,不会读取/etc/profile和~/.bash_profile、~/.bash_login和~/.profile。

因此,交互式非登录shell加载bash环境配置文件的实际过程为下图内方框中所示:

Bash启动时加载配置文件过程

例如,执行不带"--login"的bash命令或su命令时。

[root@linuxidc~]# bash /etc/profile.d/*.sh goes /etc/bashrc goes ~/.bashrc goes

[root@linuxidc~]# su /etc/profile.d/*.sh goes /etc/bashrc goes ~/.bashrc goes

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/13529.html