macOS 終端機預設開啟的是 Login Shell,因此會讀取 ~/.bash_profile(或 ~/.zprofile),而不會自動讀取 ~/.bashrc。若希望終端機啟動時也載入 .bashrc 中的別名、環境變數等設定,需在 .bash_profile 中手動引入。
編輯 ~/.bash_profile(若不存在請新建),加入以下程式碼:
[[ -s ~/.bashrc ]] && source ~/.bashrc
說明: [[ -s ~/.bashrc ]] 判斷 .bashrc 檔案存在且大小大於 0,條件成立時才執行 source ~/.bashrc 載入設定。
~/.zprofile 並引入 ~/.zshrc。echo '[[ -s ~/.bashrc ]] && source ~/.bashrc' >> ~/.bash_profile && source ~/.bash_profile
執行後立即生效,後續開啟終端機皆會自動讀取 .bashrc。