为了全局设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~~~

猜你喜欢

转载自blog.csdn.net/github_36487770/article/details/80334162