How To Upgrade your Bash Version on Mac OS?

  • HOME
  • >
  • MAC
  • >
  • How To Upgrade your Bash Version on Mac OS?
Last Updated: 

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 a default shell using. The below commands 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 ${BASH_VERSION}
3.2.57(1)-release

👉 Note that the newest versions of macOS (Starting at Catalina) now uses zsh as a default instead of bash. 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 with echo $SHELL or which 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.

Related mac posts that you may like...
How To Format Date and Time in Linux, macOS, and Bash?
This guide covers how to format date and time in Linux, Mac, and the Bash shell. Includes how to do date shell operations like adding days or comparing dates.
How To Use Option as Meta Key in macOS Terminal?
The Meta Key is a modifier key that can be quite helpful to improve your productivity while working in a terminal and bash. This post cover how to enable from the command line the Meta Key in macOS Terminal.
How To Change Preferences from the Command Line on macOS?
Introduction to the command line 'defaults' on Mac with examples on how to programmatically change user preferences from a shell script and terminal.
How to Find which macOS version you are running?
Find out which macOS version you are currently running with the use of two command line tools named respectively system_profiler and sw_vers.