mac但是bash_profile不生效的解决方法

最终解决方案是:编辑.zshrc文件,在最后追加一个source命令

source ~/.bash_profile

我们来看原因吧:
mac在启动后用户登陆进来没有执行~/.bash_profile文件,说明就没有默认加载这个呗。
而配置“bash_profile”这个文件,是我们在Linux下的习惯。
带着好奇心,百度了一下,网上说跟默认shell有关系。
那就查一下呗,可以使用echo $0来查看
这是Centos下的:
可以看到默认shell是bash

[root@TrueDei ~]#
[root@TrueDei ~]# echo $0
-bash
[root@TrueDei ~]#
[root@TrueDei ~]#

这是mac下的:
而在mac下的默认shell是zsh。

zhenghui@192 ~ % echo $0
-zsh
zhenghui@192 ~ %

bash加载配置文件的顺序:

登陆式SHELLL配置文件加载顺序:/etc/profile > .bash_profile > .bash_login > .profile > .bash_logout.
非登录式SHELL配置文件加载顺序:/etc/bash.bashrc > .bashrc
注: 先加载的配置文件的配置,可能会被后加载的配置所覆盖

zsh加载配置文件的顺序:

$ZDOTDIR/.zshenv
$ZDOTDIR/.zprofile
$ZDOTDIR/.zshrc
$ZDOTDIR/.zlogin
$ZDOTDIR/.zlogout
${TMPPREFIX}* (default is /tmp/zsh*)
/etc/zshenv
/etc/zprofile
/etc/zshrc
/etc/zlogin
/etc/zlogout (installation-specific - /etc is the default)

而加载顺序是这样的:

/etc/zshenv
~/.zshenv
/etc/zprofile
~/.zprofile
/etc/zshrc
~/.zshrc
/etc/zlogin
~/.zlogin
~/.zlogout
/etc/zlogout

我们比较关心的是用户下的,过滤一些我们不必关心的:

~/.zshenv
~/.zprofile
~/.zshrc
~/.zlogin
~/.zlogout

在bash中我们可以看到咱们熟悉的“bash_profile”配置文件,但是在zsh中始终没有看到“bash_profile”的影子。
那我们就去找一下mac下zsh用户下的这些配置文件吧。

可以看到只有“.zshrc”文件,可以得出一个解决,那么用户下默认执行的肯定就是“.zshrc”文件了

zhenghui@192 ~ %
zhenghui@192 ~ % ls -a |egrep  -e "zshenv|zprofile|zshrc|zlogin|zlogout"
.zshrc
zhenghui@192 ~ %
zhenghui@192 ~ %

编辑.zshrc文件,在最后追加一个source命令

source ~/.bash_profile

我们还可以查看系统中已有的shell

zhenghui@192 ~ % cat /etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

还可以更改默认shell
通过chsh命令改变当前的shell。当前的shell 设置为/bin/bash,改变shell的设置/bin/csh。
通过 -s 参数改变当前的shell设置

zhenghui@192 ~ % chsh -s /bin/csh  /bin/csh

暂无评论

相关推荐

sed查找替换字符串

直接上伪代码 sed -i "s/查找字段/替换字段/g" 示例: //Readme.tx文件内容 appname="我的天气" g …

微信扫一扫,分享到朋友圈

mac但是bash_profile不生效的解决方法