MacOS comes with bash 3 as the default shell program; Apple doesn't want to upgrade to the later versions for intellectual property reasons. This means that some features of later versions of bash, like associative arrays, are not available with the default shell. You can always install later versions yourself, however.
Setting up my Mac to run a different version of bash in the terminal was surprisingly hard for me to figure out how to do; it's a pretty straightforward process but I didn't see any one resource describing the entire thing from start to finish, so I'm doing it here.
This is based on my own experience installing bash 5.0.16, using Homebrew 2.2.7, on MacOS Mojave 10.14.6. If this doesn't work for you, let me know in the comments (including the versions of bash, Homebrew, and MacOS), and what if anything did end up working. I'd love to make it applicable to more people.
This tutorial assumes some basic familiarity using the terminal on Mac.
You should have the Homebrew package manager installed -- see this article for instructions.
There's some basic editing with vim -- this article contains everything you need here, but it's basically just knowing
i for "insert" and
return to save changes.
Verify that you have the old version installed currently:
$ echo $BASH_VERSION 3.2.57(1)-release
$ brew install bash to install the latest version (or specify a version). Take note of where in the filesystem the cask is located, you'll see a message like this:
==> Pouring bash-5.0.16.mojave.bottle.tar.gz 🍺 /usr/local/Cellar/bash/5.0.16: 150 files, 9.4MB
Take a look at that folder:
$ ls /usr/local/Cellar/bash/5.0.16 AUTHORS NEWS changelog CHANGES NOTES include COPYING README lib INSTALL_RECEIPT.json bin share $ ls /usr/local/Cellar/bash/5.0.16/bin bash bashbug
The full path to the shell we want is (in this case)
/usr/local/Cellar/bash/5.0.16/bin/bash. Make a note of it for the next step.
There are two steps to changing this. First, find the "shells" file and edit it:
$ atom /etc/shells # or whatever your preferred text editor is
Add the full path (in this case
/usr/local/Cellar/bash/5.0.16/bin/bash) to the end of the list of shells, then save and quit. This will tell your computer that it is safe to use that shell (it's a safety measure to prevent people from accidentally setting random programs as their default shell and breaking everything).
Back in your command line, use the "change shell" command:
$ chsh Changing shell for <user>. Password for <user>:
Enter your password. You'll then get file like this in Vim:
# Changing user information for <user>. # Use "passwd" to change the password. ## # Open Directory: /Local/Default ## Shell: /bin/bash Full Name: Office Location: Office Phone: Home Phone: ~ ~
Change the entry after "shell" to the new shell path, then save and exit.
You may need to open a new terminal window or tab for this to work.
$ echo $BASH_VERSION 5.0.16(1)-release
🥳 Yay! 🥳 You can now use new features from bash 4 and 5. And you can always put the default shell back using
chsh if you want.