How To Upgrade your Bash Version on MacOS

Shell Tips
January 16, 2020 | COMMENTS

GNU Bash is a powerful shell, unfortunately most distribution don't provide you with the latest version which may prevent you to take advantage of the great new features that came with Bash 4 and 5. Also, running an outdated bash version probably expose you to some major vulnerabilities. For example, MacOS comes with Bash 3.x which is quite limiting and lack key features like associative arrays (dictionnaries), improved auto-completion, better Posix conformance, etc. This post cover simple steps to upgrade bash on MacOS.

Check Your Current Bash Version

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

$ echo ${BASH_VERSION}
3.2.57(1)-release

Upgrade with Homebrew

If you are not using Homebrew yet, you should. It's a great package management for MacOS, simply install it with this command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Then, simply upgrade, install, and reload bash.

$ brew upgrade
Updating Homebrew...

$  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 bash, we can simply use exec as we saw in a previous post. Then, check your version again.

$ exec bash
$ 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 with echo $SHELL or which bash.

Change Your Default Bash

To go a step further, you will probably want to make this new shell your user's default shell. First, you will need to update the list of permited shells by adding the bash brew version into /private/etc/shells.

$ echo $(brew --prefix)/bin/bash | sudo tee -a /private/etc/shells
/usr/local/bin/bash

$ 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 need to update your user's shell with chpass.

$ 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, then control click on your user to select Advanced Options.... Then simply update the Login shell to /usr/local/bin/bash.