linux下 /etc/profile、~/.bash_profile ~/.profile的執行過程

來源:互聯網
上載者:User
關於登入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

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 啟動並執行通常二者設定大致相同,所以通常前者會調用後者。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.