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

macOS 終端機不讀取 .bashrc 的原因與解決方法

編輯:系統常見問題解答

macOS 終端機預設開啟的是 Login Shell,因此會讀取 ~/.bash_profile(或 ~/.zprofile),而不會自動讀取 ~/.bashrc。若希望終端機啟動時也載入 .bashrc 中的別名、環境變數等設定,需在 .bash_profile 中手動引入。

解決方法

編輯 ~/.bash_profile(若不存在請新建),加入以下程式碼:

[[ -s ~/.bashrc ]] && source ~/.bashrc

說明: [[ -s ~/.bashrc ]] 判斷 .bashrc 檔案存在且大小大於 0,條件成立時才執行 source ~/.bashrc 載入設定。

適用情境

  • 使用 Bash 作為預設 Shell 的 macOS 版本(Catalina 以前)。
  • Catalina 及更新版本預設 Shell 為 Zsh,請改編輯 ~/.zprofile 並引入 ~/.zshrc。

一行指令快速完成

echo '[[ -s ~/.bashrc ]] && source ~/.bashrc' >> ~/.bash_profile && source ~/.bash_profile

執行後立即生效,後續開啟終端機皆會自動讀取 .bashrc。

Copyright © Windows教程網 All Rights Reserved