如何让 iTerm 使用 brew 显示的较新版本的 bash?在 OSX 上更改用户的外壳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23059662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:12:12  来源:igfitidea点击:

How can I get iTerm to use the newer version of bash that brew shows? Change a user's shell on OSX

macosbashshelldefaulthomebrew

提问by Michael Durrant

When I do the brew upgradeI see I do have the newer version. How can I use it?

当我这样做时,brew upgrade我看到我确实有较新的版本。我怎样才能使用它?

$ bash -version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
$ brew upgrade bash
Error: bash-4.2.45 already installed
$ which bash
/bin/bash

I do see I have

我确实看到我有

/usr/local/Cellar/bash/4.2.45/bin

but when i do

但是当我这样做的时候

$ /usr/local/Cellar/bash/4.2.45/bin/bash

I am still in

我还在

$ bash -version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.
08:06:45 mdurrant w89123148q1 /usr/local/Cellar/bash/4.2.45/bin master

The contents of /etc/shellsare:

内容为/etc/shells

/usr/local/Cellar/bash/4.2.45/bin/bash  # (I added this)
/usr/local/bin/bash
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

chsh didn't seem to do what I hoped:

chsh 似乎没有做我希望的事情:

$ chsh -s /usr/local/Cellar/bash/4.2.45/bin/bash
Changing shell for mdurrant.
Password for mdurrant:
chsh: /usr/local/Cellar/bash/4.2.45/bin/bash: non-standard shell
$ bash --version
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13)
Copyright (C) 2007 Free Software Foundation, Inc.

I have the file here:

我这里有文件:

$ l /usr/local/Cellar/bash/4.2.45/bin/bash
-r-xr-xr-x  1 mdurrant  admin  699688 Apr 14 19:54 /usr/local/Cellar/bash/4.2.45/bin/bash*

I've yet to actually see the new bash version for anyway that I try interactively to invoke it.

无论如何,我还没有真正看到新的 bash 版本,因为我尝试以交互方式调用它。

$ echo $BASH_VERSIONshows

$ echo $BASH_VERSION显示

3.2.51(1)-release

I tried using dscl and did

我尝试使用 dscl 并做到了

> change Local/Default/Users/mdurrant UserShell /bin/bash /usr/local/Cellar/bash/4.2.45/bin/bash

but got

但得到

<main> attribute status: eDSAttributeNotFound
<dscl_cmd> DS Error: -14134 (eDSAttributeNotFound)

and now list shows

现在列表显示

> UserShell: /usr/local/Cellar/bash/4.2.45/bin/bash

回答by mklement0

bash --version(or bash -version) will NOT report the CURRENT shell's version, but the version of the bashexecutable that comes FIRST IN THE $PATH.

bash --version(或bash -version) 不会报告当前 shell 的版本,而是在bashFIRST 中出现的可执行文件的版本$PATH

[Note: OSX 10.10 (Yosemite) is the first OSX version where /usr/local/binis placed BEFORE system paths such as /binin the $PATH. Up to 10.9, system paths came first. Thus, at the time the OP asked his question, bash --versionreported the SYSTEM's bash's version (/bin/bash), not the Homebrew-installed version (/usr/local/bin/bash)]

[注:OSX 10.10(约塞米蒂)是第一个版本OSX其中/usr/local/bin放置BEFORE系统路径,例如/bin$PATH。在 10.9 之前,系统路径是第一位的。因此,在 OP 提出问题时,bash --version报告了系统的 bash 版本 ( /bin/bash),而不是 Homebrew 安装的版本 ( /usr/local/bin/bash)]

If you want to know the current Bash shell's version, use:

如果您想知道当前的 Bash shell 版本,请使用:

echo $BASH_VERSION

In other words: your shell may well have been changed successfully - your testwas flawed.

换句话说:您的外壳很可能已成功更改 - 您的测试存在缺陷。



You canuse chshto change the current user's shell, as follows:

可以使用chsh更改当前用户的shell,如下所示:

[Update: Switched to using /usr/local/bin/bashrather than a specific, versioned path in /usr/local/Cellar/bash/<version>/bin/bash, as Homebrew will automatically keep the symlink at /usr/local/bin/bashpointed to the most recent installed version. Tip of the hat to @drevicko.]

[更新:切换到 using/usr/local/bin/bash而不是特定的版本化路径/usr/local/Cellar/bash/<version>/bin/bash,因为 Homebrew 将自动保持符号链接/usr/local/bin/bash指向最新安装的版本。向@dreviko 致敬。]

 # First, add the new shell to the list of allowed shells.
sudo bash -c 'echo /usr/local/bin/bash >> /etc/shells'
 # Change to the new shell.
chsh -s /usr/local/bin/bash 

Note that you'll be prompted for your password.
Any terminal tab/window you create from that point on will already use the new shell.

请注意,系统会提示您输入密码。
从那时起您创建的任何终端选项卡/窗口都将使用新的 shell。

Bonus tip from @bmike: If you want to replace the currentshell instance with an instance of the new shell right away, run:

@bmike 的额外提示:如果您想立即用新 shell 的实例替换当前的shell 实例,请运行:

exec su - $USER  # instantly replaces current shell with an instance of the new shell

Note that you'll be prompted for your password again.

请注意,系统将再次提示您输入密码。



Alternatively, use dscl- the OSX Directory Services CLI - to change the current user's shell; this is more cumbersome, however.

或者,使用dscl- OSX 目录服务 CLI - 更改当前用户的 shell;然而,这更麻烦

To examinethe current user's shell, use:

检查当前用户的 shell,请使用:

dscl . -read /Users/$USER UserShell  # e.g. (default): 'UserShell: /bin/bash'

or, more simply, echo $SHELL, which outputs only the file path (e.g., /bin/bash).

或者,更简单地说,echo $SHELL只输出文件路径(例如,/bin/bash)。

To changethe current user's shell to, e.g., /usr/local/bin/bash, use:

要将当前用户的 shell更改为,例如/usr/local/bin/bash,使用:

sudo dscl . -change /Users/$USER UserShell /bin/bash /usr/local/bin/bash

Note:

笔记:

  • the penultimate argument must be the value currently in effect.
  • it is NOT necessary for the new value to be contained in /etc/shellsfor interactiveuse, but the comments in /etc/shellsstate Ftpd will not allow users to connect who are not using one of these shells.
  • simply quit and restart Terminal.app(or iTerm.app) for the change to take effect - verify the new shell with echo $BASH_VERSION- a reboot is NOT required.
  • 倒数第二个参数必须是当前有效
  • 这是没有必要对于要包含在新值/etc/shells交互使用,但在评论中/etc/shells状态Ftpd will not allow users to connect who are not using one of these shells.
  • 只需退出并重新启动Terminal.app(或iTerm.app)以使更改生效 - 验证新外壳echo $BASH_VERSION- 不需要重新启动。


Explanation of errors encountered by the OP:

OP 遇到的错误的解释

  • chsh: /usr/local/Cellar/bash/4.2.45/bin/bash: non-standard shellimplies that /usr/local/Cellar/bash/4.2.45/bin/bashwas not - not yet, or not in this exact form - listed in /etc/shells.
  • <main> attribute status: eDSAttributeNotFound: this dsclerror occurs when the penultimate (next-to-last) argument specified for the -changecommand does not match the currentattribute value - it is an - admittedly strange - requirement that an attribute's current value be specified in order to change it.
  • chsh: /usr/local/Cellar/bash/4.2.45/bin/bash: non-standard shell暗示这/usr/local/Cellar/bash/4.2.45/bin/bash不是 - 还没有,或者不是以这种确切的形式 - 在/etc/shells.
  • <main> attribute status: eDSAttributeNotFounddscl当为-change命令指定的倒数第二个(倒数第二个)参数与当前属性值不匹配时,会发生此错误- 这是一个 - 诚然奇怪 - 要求指定属性的当前值以更改它。

While the question suggests that both conditions were met, I suspect that they weren't met at the righttimes, due to experimentation.

虽然这个问题表明这两个条件都得到满足,但我怀疑由于实验的原因,它们没有在正确的时间得到满足。

回答by Michael Durrant

The answer was that, yes, I needed to:

答案是,是的,我需要:

  • brew install bash
  • add the path to /etc/shells
  • use chsh -s: chsh -s /usr/local/Cellar/bash/4.2.45/bin/bash
  • possibly use dscl to set the shell, i.e. within dscltype

    > change Local/Default/Users/<username> UserShell /bin/bash /usr/local/bin/zsh

  • brew install bash
  • 将路径添加到 /etc/shells
  • 使用chsh -schsh -s /usr/local/Cellar/bash/4.2.45/bin/bash
  • 可能使用 dscl 来设置外壳,即在dscl类型内

    > change Local/Default/Users/<username> UserShell /bin/bash /usr/local/bin/zsh

most importantly:

最重要的是:

  • quit the terminal (really close the app, not just its windows).
  • reboot
  • 退出终端(真正关闭应用程序,而不仅仅是它的窗口)。
  • 重启

echoing $BASH_VERSIONafter rebooting showed 4.2.45(2)-release

$BASH_VERSION重启后回显显示4.2.45(2)-release

回答by Lri

You shouldn't have to do anything else than to run:

除了运行之外,您不应该做任何其他事情:

echo /usr/local/bin/bash|sudo tee -a /etc/shells;chsh -s /usr/local/bin/bash

After that iTerm and Terminal should use /usr/local/bin/bashfor new shells.

之后 iTerm 和终端应该/usr/local/bin/bash用于新的外壳。

chsh, dscl, and the Users & Groups preference pane all modify /var/db/dslocal/nodes/Default/users/$USER.plist.

chshdscl、 和用户和组首选项窗格都修改了/var/db/dslocal/nodes/Default/users/$USER.plist

回答by n.caillou

Current versions of iTerm2 allow for a simpler & less intrusive change. In Preferences > Profiles > General, under "Command" change "Login shell" to eg.:

当前版本的 iTerm2 允许进行更简单且侵入性更低的更改。在首选项> 配置文件> 常规中,在“命令”下将“登录外壳”更改为例如:

/usr/local/bin/bash -l