bash&shell系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html
當用戶登錄系統時,會加載各種bash配置文件,還會設置或清空一系列變量,有時還會執行一些自定義的命令。這些行為都算是啟動bash時的過程。
另外,有些時候登錄系統是可以交互的(如正常登錄系統),有些時候是無交互的(如執行一個腳本),因此總的來說bash啟動類型可分為交互式shell和非交互式shell。更細分一層,交互式shell還分為交互式的登錄shell和交互式非登錄shell,非交互的shell在某些時候可以在bash命令后帶上"--login"或短選項"-l",這時也算是登錄式,即非交互的登錄式shell。
1.1 判斷是否交互式、是否登錄式
判斷是否為交互式shell有兩種簡單的方法:
方法一:判斷變量"-",如果值中含有字母"i",表示交互式。
[root@xuexi ~]# echo $- himBH [root@xuexi ~]# vim a.sh #!/bin/bash echo $- [root@xuexi ~]# bash a.sh hB
方法二:判斷變量PS1,如果值非空,則為交互式,否則為非交互式,因為非交互式會清空該變量。
[root@xuexi ~]# echo $PS1 [\u@\h \W]\$
判斷是否為登錄式的方法也很簡單,只需執行"shopt login_shell"即可。值為"on"表示為登錄式,否則為非登錄式。
[root@xuexi ~]# shopt login_shell
login_shell on
[root@xuexi ~]# bash [root@xuexi ~]# shopt login_shell login_shell off
所以,要判斷是交互式以及登錄式的情況,可簡單使用如下命令:
echo $PS1;shopt login_shell
# 或者
echo $-;shopt login_shell
1.2 幾種常見的bash啟動方式
(1).正常登錄(偽終端登錄如ssh登錄,或虛擬終端登錄)時,為交互式登錄shell。
[root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell on
(2).su命令,不帶"--login"時為交互式、非登錄式shell,帶有"--login"時,為交互式、登錄式shell。
[root@xuexi ~]# su root [root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell off
[root@xuexi ~]# su - Last login: Sat Aug 19 13:24:11 CST 2017 on pts/0 [root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell on
(3).執行不帶"--login"選項的bash命令時為交互式、非登錄式shell。但指定"--login"時,為交互式、登錄式shell。
[root@xuexi ~]# bash [root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell off
[root@xuexi ~]# bash -l [root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$ login_shell on
(4).使用命令組合(使用括號包圍命令列表)以及命令替換進入子shell時,繼承父shell的交互和登錄屬性。
[root@xuexi ~]# (echo $BASH_SUBSHELL;echo $PS1;shopt login_shell) 1 [\u@\h \W]\$ login_shell on
[root@xuexi ~]# su [root@xuexi ~]# (echo $BASH_SUBSHELL;echo $PS1;shopt login_shell) 1 [\u@\h \W]\$ login_shell off
(5).ssh執行遠程命令,但不登錄時,為非交互、非登錄式。
[root@xuexi ~]# ssh localhost 'echo $PS1;shopt login_shell' login_shell off
(6).執行shell腳本時,為非交互、非登錄式shell。但指定了"--login"時,將為非交互、登錄式shell。
例如,腳本內容如下:
[root@xuexi ~]# vim b.sh #!/bin/bash echo $PS1 shopt login_shell
不帶"--login"選項時,為非交互、非登錄式shell。
[root@xuexi ~]# bash b.sh login_shell off
帶"--login"選項時,為非交互、登錄式shell。
[root@xuexi ~]# bash -l b.sh login_shell on
(7).在圖形界面下打開終端時,為交互式、非登錄式shell。
但可以設置為使用交互式、登錄式shell。
1.3 加載bash環境配置文件
無論是否交互、是否登錄,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@xuexi ~]# 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@xuexi ~]# grep -A 1 \~/\.bashrc ~/.bash_profile if [ -f ~/.bashrc ]; then . ~/.bashrc fi
而~/.bashrc中又有加載/etc/bashrc的命令。
[root@xuexi ~]# 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環境配置文件的實際過程如下圖:
以下結果驗證了結論:
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@xuexi ~]# 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@xuexi ~]# bash -l # 執行帶有"--login"選項的login時 /etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes
[root@xuexi ~]# su - # su帶上"--login"時 /etc/profile.d/*.sh goes /etc/profile goes /etc/bashrc goes ~/.bashrc goes ~/.bash_profile goes
[root@xuexi ~]# vim a.sh # 執行shell腳本時帶有"--login"時 #!/bin/bash -l echo haha [root@xuexi ~]# ./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環境配置文件的實際過程為下圖內方框中所示:
例如,執行不帶"--login"的bash命令或su命令時。
[root@xuexi ~]# bash /etc/profile.d/*.sh goes /etc/bashrc goes ~/.bashrc goes
[root@xuexi ~]# su /etc/profile.d/*.sh goes /etc/bashrc goes ~/.bashrc goes
③.非交互式、非登錄式shell啟動bash時,不會加載前面所說的任何bash環境配置文件,但會搜索變量BASH_ENV,如果搜索到了,則加載其所指定的文件。但並非所有非交互式、非登錄式shell啟動時都會如此,見情況④。
它就像是這樣的語句:
if [ -n "$BASH_ENV" ];then . "$BASH_ENV" fi
幾乎執行所有的shell腳本都不會特意帶上"--login"選項,因此shell腳本不會加載任何bash環境配置文件,除非手動配置了變量BASH_ENV。
④.遠程shell方式啟動的bash,它雖然屬於非交互、非登錄式,但會加載~/.bashrc,所以還會加載/etc/bashrc,由於是非登錄式,所以最終還會加載/etc/profile.d/*.sh,只不過因為是非交互式而使得執行的結果全部重定向到了/dev/null中。
如果了解rsync,就知道它有一種遠程shell連接方式。所謂的遠程shell方式,是指通過網絡的方式啟動bash並將bash的標准輸出關聯起來,就像它連接了一個遠程的shell守護進程一樣。一般由sshd實現這樣的連接方式,老版的rshd也一樣支持。
事實也確實如此,使用ssh連接但不登錄遠程主機時(例如只為了執行遠程命令),就是遠程shell的方式,但它卻是非交互、非登錄式的shell。
[root@xuexi ~]# ssh localhost echo haha root@localhost's password: /etc/bashrc goes ~/.bashrc goes haha
正如上文所說,它同樣加載了 /etc/profile.d/*.sh ,只不過/etc/bashrc中的if判斷語句 if [ "$PS1" ]; then 使得非交互式的shell要將執行結果重定向到/dev/null中。