Mac配置環(huán)境變量的地方
1./etc/profile ? (建議不修改這個(gè)文件 )
全局(公有)配置,不管是哪個(gè)用戶,登錄時(shí)都會(huì)讀取該文件。
2./etc/bashrc ? ?(一般在這個(gè)文件中添加系統(tǒng)級(jí)環(huán)境變量)
全局(公有)配置,bash shell執(zhí)行時(shí),不管是何種方式,都會(huì)讀取此文件。
3.~/.bash_profile ?(一般在這個(gè)文件中添加用戶級(jí)環(huán)境變量)
每個(gè)用戶都可使用該文件輸入專(zhuān)用于自己使用的shell信息,當(dāng)用戶登錄時(shí),該文件僅僅執(zhí)行一次!
OS X系統(tǒng)的環(huán)境變量,加載順序?yàn)椋?/p>
/etc/profile
/etc/paths
~/.bash_profile
~/.bash_login
~/.profile
~/.bashrc
/etc/profile和/etc/paths是系統(tǒng)級(jí)別的,系統(tǒng)啟動(dòng)就會(huì)加載,
后面幾個(gè)是當(dāng)前用戶級(jí)的環(huán)境變量。
~/.bash_profile,~/.bash_login,~/.profile按照從前往后的順序讀取,
如果~/.bash_profile文件存在,則后面的幾個(gè)文件就會(huì)被忽略不讀了,
如果~/.bash_profile文件不存在,才會(huì)以此類(lèi)推讀取后面的文件。
~/.bashrc沒(méi)有上述規(guī)則,它是bash shell打開(kāi)的時(shí)候載入的。
設(shè)置PATH的語(yǔ)法為:
export PATH="$PATH:<PATH 1>:<PATH 2>:<PATH 3>:...:<PATH N>"
注:
(1)一般環(huán)境變量更改后,重啟后才可生效。如果想立刻生效,則可執(zhí)行下面的語(yǔ)句:
$ source 相應(yīng)的文件
(2)如果默認(rèn)shell是bash,那么shell啟動(dòng)時(shí)會(huì)觸發(fā).bashrc,如果默認(rèn)shell是zsh,那么shell啟動(dòng)時(shí)會(huì)觸發(fā).zshrc
(3)環(huán)境變量既可以加到$PATH頭部,也可以加到$PATH尾部。
例如mac中自帶emacs的位置在/usr/local/emacs
$ type emacs
emacs is /usr/local/emacs
如果在.zshrc中添加export PATH="$PATH:/Applications/Emacs.app/Contents/MacOS"
且source .zshrc之后,type emacs還是emacs is /usr/local/emacs
解決方案是,把路徑加在$PATH頭部,
export PATH="/Applications/Emacs.app/Contents/MacOS:$PATH"
或者增加alias,
alias emacs="/Applications/Emacs.app/Contents/MacOS/Emacs"