最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中.bashrc中的export的PATH没生效

Mac crifan 8784浏览 0评论

折腾:

【已解决】Mac本地安装Python3的pipenv虚拟环境

【已解决】pipenv安装后警告:The script virtualenv is installed in which is not on PATH

期间,已经把:

python3.6的path,其中包括pipenv,添加到PATH中,且放到.bashrc中了:

<code>➜  robotDemo echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
➜  robotDemo cat ~/.bashrc

export NVM_DIR="/Users/crifan/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] &amp;&amp; . "$NVM_DIR/nvm.sh"  # This loads nvm

export PATH="/Users/crifan/Library/Python/3.6/bin:$PATH"
</code>

但是每次启动后,iTerm中的Mac的shell中,还是没有找到pipenv:

<code>➜  robotDemo pipenv shell
zsh: command not found: pipenv
</code>

感觉是:

~/.bashrc

并没有生效啊

mac bashrc 无效

mac下~/.bashrc不起作用 – CSDN博客

mac中添加环境变量无效 – CSDN博客

去此处mac的iTerm中新开一个tab的shell

看看此处.bashrc中的export的path是否有效

依旧是没用的。

所以此处:

新开iTerm的tab-》新打开一个shell

并没有执行.bashrc

再去打开mac自带的shell试试

问题依旧。

这些几个帖子都在说什么

login shell

和非login shell

那此处的Mac的shell/iTerm到底是属于什么shell?

macOS .bashrc 无效问题 | tracenote

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

Mac OS X — an exception

An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile instead of .bashrc. Other GUI terminal emulators may do the same, but most tend not to.

If you want your aliases to work in both login and non-login shells (and you usually do), you should put them in .bashrc and source .bashrc in your .bash_profile, with a line like this:

1

[ -r ~/.bashrc ] && source ~/.bashrc

此处已经加到.bashrc了,

去看看.bash_profile

<code>➜  ~ cat ~/.bash_profile

# added by Anaconda2 5.0.1 installer
export PATH="/Users/crifan/anaconda2/bin:$PATH"
</code>

那么就去加上:

<code>➜  ~ vi ~/.bash_profile
➜  ~ cat ~/.bash_profile

# added by Anaconda2 5.0.1 installer
export PATH="/Users/crifan/anaconda2/bin:$PATH"

[ -r ~/.bashrc ] &amp;&amp; source ~/.bashrc
</code>

然后重新打开一个iTerm的tab,看看是否生效

结果没有:

难道是需要source .bash_profile ?

算了,如果试了,那么PATH肯定就对了,后续没法继续知道是否配置文件真的生效。

Mac OS X 环境变量设置【转】 – Helix的日志 – 网易博客

“OS X 和 Linux 都有 .bash_profile 和 .bashrc 这两个配置文件。

但是,在OS X里会遇到bashrc不生效的情况。

当shell是login shell,.bash_profile才会加载,而bashrc正好相反。

真正的区别是在linux下,当用户登录到一个图形界面,然后打开一个终端terminal,那些shell是non-login shell。

然而,在OS X登录的时候,并没有运行着一个shell,所以,在运行Terminal.app的时候,其实那是一个login shell。

你还是可以在bashrc下写一些配置,只要在bash_profile里source .bashrc就是了。

[ -r ~/.bashrc ] && source ~/.bashrc”

还是重启Mac试试吧

重启后去看看

<code>➜  ~ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
➜  ~ cat ~/.bash_profile

# added by Anaconda2 5.0.1 installer
export PATH="/Users/crifan/anaconda2/bin:$PATH"

[ -r ~/.bashrc ] &amp;&amp; source ~/.bashrc

➜  ~ cat ~/.bashrc

export NVM_DIR="/Users/crifan/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] &amp;&amp; . "$NVM_DIR/nvm.sh"  # This loads nvm

export PATH="/Users/crifan/Library/Python/3.6/bin:$PATH"

</code>

并没有生效。

参考:

https://blog.csdn.net/hlllmr1314/article/details/52228672

去把:

source ~/.bash_profile

加到zsh中:

<code>➜  ~ vi ~/.zshrc
➜  ~ cat ~/.zshrc
# Path to your oh-my-zsh installation.
export ZSH=/Users/crifan/.oh-my-zsh
。。。
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"

source ~/.bash_profile
</code>

然后重新打开iTerm的tab试试,果然就可以了:

<code>Last login: Fri Apr 20 11:46:54 on ttys009
➜  ~ echo $PATH
/Users/crifan/Library/Python/3.6/bin:/Users/crifan/.nvm/versions/node/v5.4.1/bin:/Users/crifan/anaconda2/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
➜  ~
</code>

mac自带的shell终端也是可以的:

【总结】

此处Mac中,安装了zsh的前提下,.bashrc配置不生效。

解决办法最终是:

1.把:

<code>[ -r ~/.bashrc ] &amp;&amp; source ~/.bashrc
</code>

加到:

~/.bash_profile

的最后一行

2.把

<code>source ~/.bash_profile
</code>

加到

~/.zshrc

的最后一行。

即可:

新开iTerm的tab后:

<code>echo $PATH
</code>

就可以看到希望的PATH包含了.bashrc中export的路径了。

转载请注明:在路上 » 【已解决】Mac中.bashrc中的export的PATH没生效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.323 seconds, using 22.10MB memory