Linux系統環境變數檔案解析(etc/profile ,/etc/bashrc ,~/.bash_profile)

程式設計本就是逆天而行發表於2020-11-06

在不同的 Linux 系統下,以下檔案可能沒有或是檔名不同,/etc/profile 中設定的變數(全域性)的作用於所有使用者,~/.bashrc 等設定的變數(區域性)只能繼承 /etc/profile 中的變數。

  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 時,該檔案被讀取。新增變數使用 PATH=$PATH:/usr/local/bin/nodejs ,使用 : 分割表示並列。

  5. ~/.bash_logout
    當每次退出系統(退出 bash shell)時,執行該檔案。


呼叫順序
登入且登入 shell 是bash時
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile

登入 bash 退出時
~/.bash_logout

當啟動一個互動的 bash 時
~/.bashrc

相關文章