先了解Mac環境變量加載順序
/etc/profile
/etc/paths
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc
其中/etc/profile
和/etc/paths
是系統級別的,系統啟動就會加載,
后幾個是當前用戶級的環境變量。后面3個按照從前往后的順序讀取,如果/.bash_profile文件存在,則后面的幾個文件就會被忽略不讀了,如果/.bash_profile文件不存在,才會以此類推讀取后面的文件。
~/.bashrc沒有上述規則,它是bash shell打開的時候載入的。
也就是說在當前用戶的目錄下,如果有了.bash_profile文件就不會去加載.bashrc文件。
login shell
vsno login shell
- login shell
需要輸入用戶名和密碼,進入的 shell就是loginshell。
注意:在shell里使用su - user_name 切換用戶,登錄的shell也是login shell
login shell會讀取的文件有:/etc/profile
、~/.bash_profile
,~/.bash_login
,~/.profile
,按以上順序讀取。 - no login shell。
在登錄shell時不輸入輸入用戶名和密碼,那么這個就是no login shell。
在圖形界面下,打開一個shell也是no login shell
no login shell 在打開的時候,執行的文件是:~/.bashrc
,而.bashrc
又會執行/etc/bashrc
文件。
解決方案:要加載.bashrc文件,需要在.bash_profile文件中直接加載該文件。
在.bash_profile文件中加入以下代碼:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
改完需要source 命令生效
參考
上述修改時針對終端時bash的修改,假如終端是zsh
的話,需要找到用戶目錄下的.zlogin
文件,加入上述代碼