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

/etc/profile:登入 Shell 讀取,設定全域變數,並讀取 /etc/profile.d/*.sh。/etc/bashrc:每次啟動互動式 bash 讀取(非登入 Shell)。~/.bash_profile:登入 Shell 讀取一次,通常調用 ~/.bashrc。(Ubuntu 預設為 ~/.profile)~/.bashrc:每次啟動互動式 bash 讀取(含開啟新終端)。建議在此加入個人別名、PATH 修改。~/.bash_logout:登出時執行。/etc/profile → ~/.bash_profile(或 ~/.bash_login、~/.profile 首個存在者) → ~/.bashrc(若被調用)。~/.bashrc → /etc/bashrc。$BASH_ENV 指定檔案。# 在 ~/.bashrc 末尾加入
export PATH=$PATH:/custom/path
修改後執行 source ~/.bashrc 立即生效,或重新登入。切記用冒號分隔,不要覆蓋原 PATH。