Real quick! Found out what version of bash you're running on your mac. You'll probably find it's circa 2007 or in tech years 'Old as Hell'.
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
With bashV5 out, let's bring our shell into the new decade!
How to upgrade:
- Install latest version of bash (via homebrew)
- Add new bash to Whitelist of shell environments
- Set new bash install as default shell
Install latest version of bash (via homebrew):
I prefer homebrew for brevity and ease:
brew instal bash
Now let's take a quick peak at the instances of bash installed:
$ which -a bash
bash is /usr/local/bin/bash # <- Recently installed version
bash is /bin/bash # <- Old version packaged with Mac
Prepackaged (or the old version of) bash will be the instance in /bin/bash
while the recently installed version of bash will be in /user/local/bin/bash
. BUT! Because you shouldn't trust strangers when it comes to your shell and development environment, let's get some verification.
$ /usr/local/bin/bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 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.
$ /bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
Proof! /usr/local/bin/bash
is the recently installed bash.
When we ran which -a bash we saw that the directory of the new version (/usr/local/bin
) comes by default before the directory of the old version (/bin
) in the PATH
variable, the version used when you just type bash
is the newest version:
Since the directory of the new version (/usr/local/bin
) comes by default before the directory of the old version (/bin
) in the PATH
variable, the version used when you just type bash
is the new one:
$ bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 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.
$ /usr/local/bin/bash --version
GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0)
Copyright (C) 2019 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.
Add new bash to Whitelist of shell environments
UNIX includes a security feature that restricts the shells that can be used as login shells (i.e. the shell used after logging in to the system) to a list of “trusted” shells. These shells are listed in the /etc/shells
file.
Since you want to use the newly installed Bash shell as the default shell, it must be able to act as a login shell. That means, you have to add it to the /etc/shells
file. You can edit this file as the root user:
$ sudo vim /etc/shells
And add the /usr/local/bin/bash
shell to its content, so that the file looks something like this:
# 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/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash
Now to wrap up, we have to set the new bash install as our default shell.
Set new bash install as default shell
At this point, if you opened a new terminal window, you would still be using Bash v3.2. This is because /bin/bash
is still set as the default shell. To change this to your new shell, execute the following command:
$ chsh -s /usr/local/bin/bash
That’s it! The default shell for your current user is now set to the new version of Bash. If you close and reopen the terminal window, you should now be using the new version already. You can verify this as follows:
$ echo $BASH_VERSION5.0.0(1)-release
The chsh
command changes the default shell only for the user who executes the command. If you want to change the default shell for other users too, you can repeat this command by assuming another user’s identity (e.g. with su
). Most importantly perhaps you might want to change the default shell for the root user, which you can do as follows:
$ sudo chsh -s /usr/local/bin/bash
In this way, if you use sudo su
to open a shell as the root user, it will also use the new Bash version.