[醉了]解决重启mac但是bash_profile不生效的彻底解决方法

简介: [醉了]解决重启mac但是bash_profile不生效的彻底解决方法

如果你懒得看整个过程,就直接:

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

zhenghui@192 ~ %
zhenghui@192 ~ % cat ~/.zshrc
#每次用户进入之后使bash_profile生效
source ~/.bash_profile
zhenghui@192 ~ %

令人蛋疼的事

换了mac也一段时间了,mac到手后,第一件事就是配置jdk和maven环境变量。但是每次想在命令行下使用到java明令或者maven明令的时候,都会提示没有安装jdk或者找不到JAVA_HOME变量。

然后不得不手动执行:source ~/.base_profile

然后再此执行之前的命令就可以了。

就这样持续了N次。

今天跟群友们讨论了一番,原来不是我自己遇到了这个问题,决定一探究竟,终于解决了。

百因必有果

我相信这一切肯定是有原因的,肯定不是命中注定。

既然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加载配置文件的顺序:

通过查看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命令

zhenghui@192 ~ %
zhenghui@192 ~ % cat ~/.zshrc
#每次用户进入之后使bash_profile生效
source ~/.bash_profile
zhenghui@192 ~ %

重启一下mac,发现可以了。

其他

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

zhenghui@192 ~ %
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
zhenghui@192 ~ %
zhenghui@192 ~ %

还可以更改默认shell

通过chsh命令改变当前的shell。当前的shell 设置为/bin/bash,改变shell的设置/bin/csh。

通过 -s 参数改变当前的shell设置

先说好::::具体我没尝试过,这是从百度上找的,大家如果尝试的话,导致电脑废了,不要赖我哈。

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

参考资料

https://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_linux_010.html

http://wxnacy.com/2018/10/08/zsh-startup-files/ 资料

http://zsh.sourceforge.net/Doc/Release/zsh_toc.html zsh文档

http://zsh.sourceforge.net/Doc/Release/Files.html#Files 配置文件

目录
相关文章
|
3月前
|
Linux Shell
mac/linux提示bash: telnet: command not found
mac/linux提示bash: telnet: command not found
|
3月前
|
Linux Shell
mac/linux执行受限:bash: ./install.sh: Permission denied
mac/linux执行受限:bash: ./install.sh: Permission denied
|
6月前
|
Shell iOS开发 MacOS
mac ~/.bash_profile 重启失效
mac ~/.bash_profile 重启失效
|
Shell 开发工具
mac添加bash_profile环境变量
mac添加bash_profile环境变量
170 0
|
Shell
MAC修改.bashrc/.bash_profile无效,默认的用户配置文件是.zshrc,
MAC修改.bashrc/.bash_profile无效,默认的用户配置文件是.zshrc,
229 0
|
Shell
【技术分享】Mac终端bash和zsh切换
【技术分享】Mac终端bash和zsh切换
236 0
|
关系型数据库 MySQL Shell
linux/mac 环境查看mysql版本时出现 :-bash: mysql: command not found
linux/mac 环境查看mysql版本时出现 :-bash: mysql: command not found
172 0
|
Shell
MAC修改.bashrc/.bash_profile无效,默认的用户配置文件是.zshrc,
MAC修改.bashrc/.bash_profile无效,默认的用户配置文件是.zshrc,
188 0
|
Shell 开发工具
mac添加bash_profile环境变量
mac添加bash_profile环境变量
231 0
MAC 打开.bash_profile
启动终端Terminal 进入当前用户的home目录 输入cd ~ 创建.bash_profile 输入touch .bash_profile 编辑.
1412 0