天天看點

/etc/profile、~/.bash_profile等幾個檔案的執行過程

/etc/profile、~/.bash_profile等幾個檔案的執行過程

 摘自:http://blog.chinaunix.net/uid-14735472-id-3190130.html

分類: LINUX

2012-04-26 17:44:55

http://blog.chinaunix.net/uid-346158-id-2130833.html

關于登入linux時,/etc/profile、~/.bash_profile等幾個檔案的執行過程。

在登入Linux時要執行檔案的過程如下:

在 剛登入Linux時,首先啟動 /etc/profile 檔案,然後再啟動使用者目錄下的 ~/.bash_profile、 ~/.bash_login或 ~/.profile檔案中的其中一個,執行的順序為:~/.bash_profile、 ~/.bash_login、 ~/.profile。如果 ~/.bash_profile檔案存在的話,一般還會執行 ~/.bashrc檔案。因為在 ~/.bash_profile檔案中一般會有下面的代碼:

if [ -f ~/.bashrc ] ; then

 . ./bashrc

           fi

  ~/.bashrc中,一般還會有以下代碼:

if [ -f /etc/bashrc ] ; then

 . /etc/bashrc

fi

是以,~/.bashrc會調用 /etc/bashrc檔案。最後,在退出shell時,還會執行 ~/.bash_logout檔案。

執 行順序為:/etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc ->/etc/bashrc -> ~/.bash_logout

關于各個檔案的作用域,在網上找到了以下說明:

(1)/etc/profile: 此檔案為系統的每個使用者設定環境資訊,當使用者第一次登入時,該檔案被執行. 并從/etc/profile.d目錄的配置檔案中搜集shell的設定。

(2)/etc/bashrc: 為每一個運作bash shell的使用者執行此檔案.當bash shell被打開時,該檔案被讀取。

(3)~/.bash_profile: 每個使用者都可使用該檔案輸入專用于自己使用的shell資訊,當使用者登入時,該檔案僅僅執行一次!預設情況下,他設定一些環境變量,執行使用者的.bashrc檔案。

(4)~/.bashrc: 該檔案包含專用于你的bash shell的bash資訊,當登入時以及每次打開新的shell時,該該檔案被讀取。

(5)~/.bash_logout: 當每次退出系統(退出bash shell)時,執行該檔案. 另外,/etc/profile中設定的變量(全局)的可以作用于任何使用者,而~/.bashrc等中設定的變量(局部)隻能繼承/etc /profile中的變量,他們是"父子"關系。

(6)~/.bash_profile 是互動式、login 方式進入 bash 運作的~/.bashrc 是互動式 non-login 方式進入 bash 運作的通常二者設定大緻相同,是以通常前者會調用後者。

我 做了個實驗,在/etc/profile,/etc/bashrc,~/.bashrc和~/.bash_profile檔案的最後追加同一個變量分别賦 予不同的值,實驗結果表明變量最後的值為~/.bash_profile裡的值。(4個檔案都沒有修改其他設定,都是安裝系統後的預設值。)

再有就是4個檔案都追加一個值到同一個檔案,開機後檢視該檔案内容的順序為:

/etc/profile

~/.bash_profile

~/.bashrc

/etc/bashrc

----------------------

其他文章:

redhat bash 初始化設定

先說明三個概念

登入shell

正常登入程式啟動的shell.既登入成功後緊接着給登入使用者啟動的shell.

非登入互動式shell

這個shell的工作方式是互動式的,等使用者輸入,然後執行,再等使用者輸入。顯然登入shell就是一個互動式shell。

如下,我們可獲得一個互動式非登入shell:

[root@localhost ~]# bash

[root@localhost ~]# pwd

/root

非互動式shell

為運作一個shell腳本啟動的shell.

以FC5的bash為例,跟shell環境配置相關的檔案以下幾個,

/etc/profile.d/*.sh

有時你會發現定義一個别名,有時好像在任意一個檔案裡定義都可以起作用,有時好像又不起作用,那是為什麼呢?這些配置檔案各自互責了什麼工作?互相的關系是怎麼樣的?跟前面介紹的不同種類的shell的關系是如何的呢?下面對每個檔案單獨進行說明。

Linux規定,當啟動一個登入shell會執行這個腳本. 測試過程如下:

把LIST的定義加到/etc/profile檔案的未尾并儲存. 如下:

alias LIST='ls -l'

把所有其它shell配置檔案或目錄改名,這樣系統就找不到那些shell腳本了,不會執行,重而避免其它配置檔案的幹擾。如下:

[root@localhost ~]# mkdir /etc/profile.bak

[root@localhost ~]# mv /etc/profile.d/* -t /etc/profile.bak/

[root@localhost ~]# mv /etc/bashrc /etc/bashrc.bak

[root@localhost ~]# mv ~/.bash_profile ~/.bash_profile.bak

[root@localhost ~]# mv ~/.bashrc ~/.bashrc.bak

互動式shell,并測試過程如下:

bash-3.1# LIST

bash: LIST: command not found

bash-3.1# exit

exit

[root@localhost ~]#

顯然啟動一個普通互動式shell的時候, shell配置檔案/etc/profile不起作用

非互動式shell, 測試過程如下:

為了驗證先寫一個測試腳本,如下:

#!/bin/bash

LIST

把這個腳本儲存為t.sh并加下可執行權限:

[root@localhost ~]# chmod a x t.sh

[root@localhost ~]# ./t.sh        

./t.sh: line 2: LIST: command not found

[root@localhost ~]# 

顯然啟動一個非互動式shell時,shell配置檔案/etc/profile不起作用

登入shell,并測試過程如下:

Last login: Wed Nov 19 10:22:23 2008 from 192.168.0.97

-bash-3.1# LIST

total 160

drwxr-xr-x  2 root root  4096 Aug 14 12:24 Desktop

-rw-r--r--  1 root root  3211 Nov  6 10:15 Session.vim

drwxr-xr-x  2 root root  4096 Nov 10 10:58 a

-rw-r--r--  1 root root   126 Nov 12 12:42 a.txt

-rw-r--r--  1 root root   261 Nov  6 15:23 a.zip

-rw-r--r--  1 root root   157 Nov  6 15:23 aa.zip

-rw-------  1 root root  1054 Aug 14 11:59 anaconda-ks.cfg

-rw-r--r--  1 root root   691 Nov 18 10:09 b.txt

-rw-r--r--  1 root root 31671 Aug 14 11:58 install.log

-rw-r--r--  1 root root  4155 Aug 14 11:50 install.log.syslog

-rw-------  1 root root 20310 Nov 17 13:51 mbox

drwxr-xr-x  2 root root  4096 Nov 17 17:22 shell

-rwxrwxrwx  1 root root    65 Nov 19 10:11 t.sh

drwxr-xr-x 14 root root  4096 Nov  5 15:34 test

-bash-3.1# 

顯然啟動一個登入shell時,shell配置檔案/etc/profile會起作用

這個檔案跟/etc/profile起作用的時機是一樣的,都是隻在啟動一個登入shell的時候才會被source,跟/etc/profile不同的是,這裡的配置隻影響單個使用者,不對其它使用者産生影響。

/etc/bashrc與~/.bashrc

從字面上我們可以了解這兩個檔案應該跟根bash相關,即 隻要你啟動了一個bash類型的shell這兩檔案的配置就将發生作用。如果你的shell是sh、csh或是ksh這兩個檔案将不起作用。按前面的介 紹,可能很會猜測/etc/bashrc與~/.bashrc的關系跟/etc/profile與~/.bash_profile的關系一樣,一個是全局 的,一個是針對單個使用者的。從結果上看确實是這樣的,但實作過程卻是不一樣的。啟動一個bash時直接source ~/.bashrc, 而這~/.bashrc裡面會source /etc/bashrc。

在fc5下這裡的腳本會在/etc/profile裡或是~/.bashrc裡同時source, 是以這裡的設定都是一些不同分類的全局環境設定。

總結在FC5下一個登入bash的環境初始全過程是:

繼續閱讀