http://blog.csdn.net/caiwenfeng_for_23/article/details/44242961 關于登入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 . /bashrc 是以,~/.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 運作的通常二者設定大緻相同,是以通常前者會調用後者。