This guide uses Upgrade to bash 4 in Mac OS X as its base, then modifies its content over time, as appropriate. A huge thanks to @hiljaa.
Bash version can be queried with the --version
flag:
$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.
The actual installation is going to happen with Homebrew, a macOS package manager.
brew install bash
After that, grab the value from the following command. This is where Homebrew installed your new Bash binary.
$ echo $(brew --prefix bash)/bin/bash
/usr/local/opt/bash/bin/bash
Note
The path above is correct for Intel-based Macs. If you have an Apple Silicon-based Mac, the path will be /opt/homebrew/opt/bash/bin/bash
.
Now we'll want to test our version of Bash. Imagine a file:
#! /bin/bash
# version-test.sh
echo $BASH_VERSION;
Make it executable and run it:
$ chmod +x ./version-test.sh
$ ./version-test.sh
3.2.57(1)-release (x86_64-apple-darwin16)
Seemingly it’s still using the old version of Bash. The trick is the shebang on the first line, it’s pointing to the old Bash path. Change it to use the new path that you saved, above.
#! /usr/local/opt/bash/bin/bash
# version-test.sh
echo $BASH_VERSION;
#! /opt/homebrew/opt/bash/bin/bash
# version-test.sh
echo $BASH_VERSION;
Now run it and it gives a newer version.
$ ./version-test.sh
5.2.21(1)-release
Again, use the new path that you saved above.
sudo bash -c "echo $(brew --prefix bash)/bin/bash >> /etc/shells"
chsh -s $(brew --prefix bash)/bin/bash
Now quit Terminal, then re-launch it.
echo $BASH_VERSION;
If it still shows the old Bash, just go to the Terminal menu → Preferences → General → Shells open with, then choose Default login shell.