Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 環境變數配置檔完整解析:系統級 vs 使用者級、載入順序、修改生效

編輯:關於 Linux

環境變數分系統級(所有用戶)與使用者級(單一用戶),對應不同配置檔,載入順序嚴格。

系統級配置檔

  • /etc/profile:登入 Shell 讀取,設定全域變數,並讀取 /etc/profile.d/*.sh。
  • /etc/bashrc:每次啟動互動式 bash 讀取(非登入 Shell)。

使用者級配置檔(家目錄下,隱藏檔)

  • ~/.bash_profile:登入 Shell 讀取一次,通常調用 ~/.bashrc。(Ubuntu 預設為 ~/.profile)
  • ~/.bashrc:每次啟動互動式 bash 讀取(含開啟新終端)。建議在此加入個人別名、PATH 修改。
  • ~/.bash_logout:登出時執行。

載入順序

  1. 登入 Shell:/etc/profile → ~/.bash_profile(或 ~/.bash_login、~/.profile 首個存在者) → ~/.bashrc(若被調用)。
  2. 非登入互動 Shell:~/.bashrc → /etc/bashrc。
  3. 非互動 Shell(腳本):讀取 $BASH_ENV 指定檔案。

修改 PATH 範例

# 在 ~/.bashrc 末尾加入
export PATH=$PATH:/custom/path

修改後執行 source ~/.bashrc 立即生效,或重新登入。切記用冒號分隔,不要覆蓋原 PATH。

Copyright © Windows教程網 All Rights Reserved