Mac下配置環境變量重啓後不生效解決(.bash_profile vs .bashrc)(bash/zsh下不加載.bashrc問題解決)

參考上一篇文章說明:http://www.cnblogs.com/EasonJim/p/6283094.htmlhtml

得知加載順序以下:shell

/etc/profilebash

/etc/pathsspa

~/.bash_profilecode

~/.bash_loginhtm

~/.profileblog

~/.bashrcget

固然/etc/profile和/etc/paths是系統級別的,系統啓動就會加載,後面幾個是當前用戶級的環境變量。後面3個按照從前日後的順序讀取,若是~/.bash_profile文件存在,則後面的幾個文件就會被忽略不讀了,若是~/.bash_profile文件不存在,纔會以此類推讀取後面的文件。~/.bashrc沒有上述規則,它是bash shell打開的時候載入的。it

也就是說在當前用戶的目錄下,若是又了.bash_profile文件就不會去加載.bashrc文件。io

因此若是要能正常加載.bashrc文件,須要在.bash_profile文件的最末尾上加入以下語句:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

參考:http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

 

然而,這個修改只是正對base的,若是你沒有修改過,上面的方法默認重啓後就能生效。

可是,如今的mac上有些使用zsh這個做爲默認的shell,因此,在啓動shell時,默認不會按上面的套路去加載。

若是想要正常加載.bashrc文件時,就要找到用戶目錄下的.zlogin文件加入以下代碼:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

便可生效。

參考思路:

http://stackoverflow.com/questions/4493063/best-practice-for-bash-start-up-files-on-a-mac?answertab=active#tab-top

http://superuser.com/questions/866683/iterm-zsh-not-reading-bashrc-or-bash-profile

相關文章
相關標籤/搜索