在MacOS( Catalina 及以后)上将默认shell从zsh更改为bash [已关闭]

ryevplcw  于 2023-10-23  发布在  Shell
关注(0)|答案(1)|浏览(131)

已关闭此问题为not about programming or software development。它目前不接受回答。

这个问题似乎不是关于a specific programming problem, a software algorithm, or software tools primarily used by programmers的。如果你认为这个问题与another Stack Exchange site的主题有关,你可以留下评论,解释在哪里可以回答这个问题。
上个月关门了。
Improve this question
我在MacOS上使用bash shell感觉很舒服。但 Catalina 把它换成了zsh
为什么?我能换回来吗?

如果是,如何?

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

ddrv8njm

ddrv8njm1#

TLDR

  • 安装所需的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)"
  • 测试它工作:关闭并重新打开您的终端程序,然后:
> 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.

深潜

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.

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

> brew install bash
:

> brew info bash
==> bash: stable 5.2.15 (bottled), HEAD
:

这里发生了什么事?在3.2.x之后,bash许可证从LGPL v2更改为v3。苹果拒绝转变为符合LGPL v3,而是将MacOS附带的bash版本修复为3.2.x。最终它变得尴尬,(2019/ Catalina )他们切换到zsh(MIT许可证)。
现在我们需要使用chsh将默认登录shell更改为新的bash,但首先我们需要定位可执行文件。
注:恼人的是,

> brew --prefix bash
/opt/homebrew/opt/bash

..所以我们暂时不要去那里。
这里是路径homebrew需要。

> 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:-}";

让我们将这些路径添加到当前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.

现在我们将使用chsh将默认登录shell更改为/opt/homebrew/bin/bash。
chsh只接受/etc/shells中列出的shell,因此编辑该文件并插入行(例如: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

现在chsh可以工作了:

chsh -s /opt/homebrew/bin/bash

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

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

> echo $BASH_VERSION
5.2.15(1)-release

......确认它有效。
然而,还有一个问题:

which bash
/bin/bash

这是因为我们前面列出的那些自制PATH没有添加到我们的shell会话中。
要修复,请编辑(如有必要,请创建)~/.bash_profile并添加以下行:

eval "$(/opt/homebrew/bin/brew shellenv)"

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

相关问题