在 MacOS(Catalina 及更高版本)上将默认 shell 从 zsh 更改为 bash

P i*_*P i 1 macos bash shell zsh

bash在 MacOS 上使用 shell 感觉很舒服。但卡特琳娜将其替换为zsh.

为什么?我可以把它切换回来吗?

如果是这样,怎么办?

这样做有意义吗?有什么问题吗?

P i*_*P i 5

总长DR

  • 安装所需的 bash:brew install bash

  • 确认路径:(/opt/homebrew/bin/bash --version如果不同,请相应调整以下内容)。

  • sudo nano /etc/shells并插入/opt/homebrew/bin/bash到列表的顶部。这样下一条指令就会成功。

  • 更改默认登录 shell:chsh -s /opt/homebrew/bin/bash

  • 关闭并重新打开终端程序,并echo $SHELL确认正在运行的 shell 确实是 /opt/homebrew/bin/bash

  • 现在进行调整,以便终端会话可以看到自制程序位置:(nano ~/.bash_profile如果它为空,它将创建它)并添加以下行:

eval "$(/opt/homebrew/bin/brew shellenv)"
Run Code Online (Sandbox Code Playgroud)
  • 测试它是否有效:关闭并重新打开终端程序,然后:
> echo $SHELL
/opt/homebrew/bin/bash  # location of the running shell program

> echo $BASH_VERSION  # version of the running shell program
5.2.15(1)-release

> which bash  # check our $PATH contains homebrew locations
/opt/homebrew/bin/bash

> bash --version  # check this is consistent with above
GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

深潜

MacOS 附带使用 LGPL v2 许可证的最新 BASH:

> which bash
/bin/bash

> /bin/bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)

您可以安装最新的bash(例如使用homebrew

> brew install bash
:

> brew info bash
==> bash: stable 5.2.15 (bottled), HEAD
:
Run Code Online (Sandbox Code Playgroud)

这里发生了什么?AFAIK 在 3.2.x 之后,bash 许可证从 LGPL v2 更改为 v3。Apple 拒绝转变为 LGPL v3 合规性,而是将bashMacOS 附带的版本修复为 3.2.x。最终它变得尴尬,(2019/Catalina)他们转而使用zsh(麻省理工学院许可证)。

现在我们需要将chsh默认登录 shell 更改为新的 bash,但首先我们需要找到可执行文件。

注意:令人烦恼的是,

> brew --prefix bash
/opt/homebrew/opt/bash
Run Code Online (Sandbox Code Playgroud)

... 是一场徒劳的追逐,因为您会深入了解自制软件如何存储软件包 (.../Cellar)、最新版本的符号链接 (.../opt) 以及公开相关的二进制文件 (.../bin)。所以我们暂时不去那里。

以下是自制程序所需的路径。

> brew shellenv
export HOMEBREW_PREFIX="/opt/homebrew";
export HOMEBREW_CELLAR="/opt/homebrew/Cellar";
export HOMEBREW_REPOSITORY="/opt/homebrew";
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin${PATH+:$PATH}";
export MANPATH="/opt/homebrew/share/man${MANPATH+:$MANPATH}:";
export INFOPATH="/opt/homebrew/share/info:${INFOPATH:-}";
Run Code Online (Sandbox Code Playgroud)

让我们将这些路径添加到当前 shell 会话中并找到可执行文件:

> which bash
/bin/bash

> bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.

> eval "$(brew shellenv)"

> which bash
/opt/homebrew/bin/bash

> bash --version
GNU bash, version 5.2.15(1)-release (aarch64-apple-darwin22.1.0)
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)

现在我们将使用chsh将默认登录 shell 更改为 /opt/homebrew/bin/bash。

chsh将只接受 中列出的 shell /etc/shells,因此编辑该文件并插入该行(例如使用sudo nano /etc/shells.

现在你应该看到:

> 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.

/opt/homebrew/bin/bash
/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Run Code Online (Sandbox Code Playgroud)

现在chsh可以工作了:

chsh -s /opt/homebrew/bin/bash
Run Code Online (Sandbox Code Playgroud)

关闭并重新打开您的终端程序。现在:

> echo $SHELL
/opt/homebrew/bin/bash

> echo $BASH_VERSION
5.2.15(1)-release
Run Code Online (Sandbox Code Playgroud)

...确认它有效。

然而,还剩下一个问题:

which bash
/bin/bash
Run Code Online (Sandbox Code Playgroud)

这是因为我们之前列出的那些自制程序路径没有添加到我们的 shell 会话中的任何位置。

要修复,请编辑(必要时创建)~/.bash_profile并添加以下行:

eval "$(/opt/homebrew/bin/brew shellenv)"
Run Code Online (Sandbox Code Playgroud)

现在重新启动终端程序,两者echo $SHELL现在which bash应该对齐。更重要的是,您的 shell 现在可以查看自制程序位置。