/etc/profile.d/*.sh
/etc/profile
/etc/bashrc
~/.bash_profile
~/.bashrc
系統環境變數主要由這幾個腳本控制,其中前三個對所有使用者生效,後兩個只對該使用者登入時生效。
既然多個檔案都能影響環境變數,實際載入順序為何?

順序由上而下:系統啟動時先讀 /etc/profile 設定基礎變數,執行 profile 的過程中又會去讀 /etc/profile.d/*.sh。
profile 檔案:

profile 跑完後接著執行 ~/.bash_profile:

可以發現 bash_profile 又呼叫了 .bashrc,並在其中再次設定變數值。
~/.bashrc:

.bashrc 又回頭呼叫 /etc/bashrc,而這個檔案主要負責設定 PS1(例如 [root@www ~]#)。之所以還會載入 /etc/profile.d/*.sh,是針對非登入使用者的情境,這部分細節影響不大。
環境變數只在使用者登入時才會生效。若使用者不登入,變數會停留在初始內容。因此,如果你希望某個服務在開機時自動啟動,不要把指令寫進上面這些檔案裡——那要改用 systemd、rc.local 或 crontab 之類的機制。