爲了全局設MongoDB的路徑,mac下修改bashrc來配環境變量的心酸歷程

昨天配Mongodb是終端手動打的export PATH=xxxxx,但還是想找一個一勞永逸的方法。過程中頗多糾結,這裏小記一筆吧。

mac下登入終端後直接進入Users/yourUserName 目錄,在這裏直接 ls - al 找.bashrc文件或者.base_profile文件。然並卵,沒找到。touch .base_profile && vim .base_profile 後加入配置全局路徑的那一行代碼(我的是export PATH=/Users/gege/playground/mongodb/bin:$PATH),然後重啓終端,執行Mongo, 命令未找到。同樣的方法嘗試新建 && 改bashrc文件也沒用。但旁邊的小姐姐說她的MAC PRO這麼做可以,那我就不知道了。可能電腦上的一些配置啊命令啊不一樣吧。

.bash_profile和bashrc的區別

.bash_profile is executed for login shells, while .bashrc is executed for interactive non-login shells.

When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

But, if you’ve already logged into your machine and open a new terminal window (xterm) then .bashrc is executed before the window command prompt. .bashrc is also run when you start a new bash instance by typing /bin/bash in a terminal.

On OS X, Terminal by default runs a login shell every time, so this is a little different to most other systems, but you can configure that in the preferences.

晚上跟朋友語音,他遠程場外支援了一下,提醒我到etc去找,不能新建,新建的是不起作用的。etc是系統配置,具體到每個用戶是用戶環境變量。修改可參照Mac OSX 環境變量修改

今天早上來,果然在etc下面找到了bashrc,還找到了一個bashrc_Apple_Terminal, 兩個都看了下,bashrc的內容很簡單, bashrc_Apple_Terminal裏面很多註釋,感覺應該用第一個。於是改,記住一定要sudo vim bashrc,否則保存不了,然後它還是會報readonly warnning, 無視之,最後保存的時候esc按完了這麼做:

  1. 輸入:set noreadonly
  2. 加個感嘆號來override,輸入 :wq!

就可以了。

改的時候一開始誤操作了,也不知道動了哪裏,反正一打開終端給我報了個語法錯誤。真是要哭了,手賤啊!!!後來小夥伴查了下他的文件,發現是少了個空格。總之我的bashrc正確的是長這樣的,存個檔吧:

# System-wide .bashrc file for interactive bash(1) shells.
if [ -z "$PS1" ]; then
   return
fi

PS1='\h:\W \u\$ '
# Make bash check its window size after a process completes
shopt -s checkwinsize

[ -r "/etc/bashrc_$TERM_PROGRAM" ] && . "/etc/bashrc_$TERM_PROGRAM"

export PATH=/Users/gege/playground/mongodb/bin:$PATH

~              

end of story~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章