GNU Bash is a powerful shell. Unfortunately the Mac OS operating systems doesn’t provide the latest version which may prevent you to take advantage of the latest features that came with Bash 4 and 5. Also, running an outdated bash version probably expose you to some major vulnerabilities.
Mac OS comes with Bash version 3 which is quite limiting and lack key features like the bash associative arrays, improved auto-completion, better Posix conformance, etc.
This post cover simple steps on how to upgrade bash on MacOS.
👉 Update: You can read more about Bash version 5 with my post What’s New in GNU Bash 5?.
How to check Your Current Bash Version on Mac?
First and foremost, you need to know which version you are currently running as your default shell. You can use the bash --version
command or the
environment variables $BASH_VERSINFO
and $BASH_VERSION
. The $BASH_VERSINFO
variable is a
bash array which contains the major, minor, patch, and build numbers of the currently running Bash instance.
$BASH_VERSINFO[0] | The major version number (the release) |
$BASH_VERSINFO[1] | The minor version number (the version) |
$BASH_VERSINFO[2] | The patch level |
$BASH_VERSINFO[3] | The build version |
$BASH_VERSINFO[4] | The release status (e.g., beta1) |
$BASH_VERSINFO[5] | The value of MACHTYPE |
The below example shows a bash version 3.2
which comes with all Mac OS as Apple won’t distribute newer versions due to licences concerns.
[me@mac: ~]$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.
[me@mac: ~]$ echo Version=${BASH_VERSION} Major_Version=${BASH_VERSINFO[0]}
Version=3.2.57(1)-release Major_Version=3
👉 Note that the newest versions of macOS (Starting at Catalina) now uses
zsh
as a default instead ofbash
. Though,bash
version 3 is still installed and available. To find out which macOS version you are running, check out my post How to Find which Mac OS version you are running?.
Upgrade Bash on Mac with Homebrew
If you are not yet using Homebrew, start doing so. It is a great package management on Mac which will simplify a lot of the steps for you.
You can install homebrew with the below command.
[me@mac: ~]$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then, upgrade, install, and reload bash.
[me@mac: ~]$ brew upgrade
Updating Homebrew...
[me@mac: ~]$ brew install bash
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/cask).
No changes to formulae.
==> Downloading https://homebrew.bintray.com/bottles/bash-5.0.11.mojave.bottle.tar.gz
==> Downloading from https://akamai.bintray.com/01/0...
######################################################################## 100.0%
==> Pouring bash-5.0.11.mojave.bottle.tar.gz
🍺 /usr/local/Cellar/bash/5.0.11: 150 files, 9.4MB
Reload and Verify
To
reload the bash shell, we will use the exec
command. Then, check the shell version again.
[me@mac: ~]$ exec bash
[me@mac: ~]$ 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>
⚠️ Be careful: Your old bash 3 version would still be installed in
/bin/bash
while the brew version would be in/usr/local/bin/bash
. You can check which version you are using withecho $SHELL
orwhich bash
.
Change The Default Bash on Mac
To go a step further, you will want to make your new shell with Bash version 5 to be your user’s default shell.
First, you will need to update the list of permitted shells by adding the bash brew version into /private/etc/shells
. You can do this by editing directly the file or using the tee -a
command as shown below.
[me@mac: ~]$ echo $(brew --prefix)/bin/bash | sudo tee -a /private/etc/shells
/usr/local/bin/bash
[me@mac: ~]$ cat /private/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/ksh
/bin/sh
/bin/tcsh
/bin/zsh
/usr/local/bin/bash
Finally, you will need to update your user’s shell with the chpass
command line.
[me@mac: ~]$ sudo chpass -s /usr/local/bin/bash johndoe
Changing shell for johndoe.
Alternatively, instead of using chpass
, you can go to the Menu > System Preferences...
> Users & Groups
. Unlock the pane, control click on your user to select Advanced Options...
, then update the Login shell
to /usr/local/bin/bash
.