Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux 環境變數設定檔完整架構:系統級 vs 使用者級、登入 vs 互動式 Shell 載入順序圖解

編輯:Linux 系統常見問題解答
層級檔案生效範圍載入時機修改生效方式
系統級/etc/profile所有使用者登入 Shell (第一次登入)重啟機器或重新登入
/etc/bashrc所有使用者每次開啟互動式 bash重新開啟終端機或 source /etc/bashrc
使用者級~/.bash_profile當前使用者登入 Shell (僅一次)重新登入
~/.bashrc當前使用者每次開啟互動式 bash重新開啟終端機或 source ~/.bashrc

載入順序 (登入 Shell)

/etc/profile → /etc/profile.d/*.sh → ~/.bash_profile → ~/.bashrc → /etc/bashrc

關鍵差異

  • /etc/profile:系統級環境變數 (PATH、JAVA_HOME 等),需重啟/重新登入生效。
  • /etc/bashrc:系統級別名、提示字元、函數定義,即時生效。
  • ~/.bash_profile:使用者級環境變數,通常呼叫 ~/.bashrc。
  • ~/.bashrc:使用者級別名與函數,即時生效。

實務建議: 永久環境變數改 /etc/profile 或 ~/.bash_profile;別名/函數/提示字元改 /etc/bashrc 或 ~/.bashrc;修改後 source 對應檔案即時生效,免重啟。

Copyright © Windows教程網 All Rights Reserved