Updating Bash on Mac and Not Looking Back
This article is how you can update your version of Bash on the macOS terminal, and completely get rid of the old version (something other articles lack). Even in 2021, Apple is shipping computers with a version of Bash that was last updated in 2007. Don’t believe me? Type “bash --version” in terminal to see for yourself!
By upgrading bash versions, you get access to a lot of new tools if you are programming in it or using it extensively. If you don’t really use Bash, this article may not be for you, but you’re still welcome to following through on it.
First, we need to install Homebrew if you don’t have it installed already. Homebrew is the terminal package manager for macOS. Similar to apt-get or pacman on various versions of GNU/Linux distributions. You can do this by visiting https://brew.sh or by copying this line of code into your terminal (it may take a few minutes and will ask you for your password)::
/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
After that’s installed, type the following line to make sure everything is working before we get started.
brew doctor
Now that everything is working, we can get started by installing the latest version of bash from the package manager
brew install bash
To verify the installation, you would see 2 paths with the following command:
which -a bash
The first one at /usr/local/bin/bash is the newly installed version, and /bin/bash is the one we want to replace one.
/usr/local/bin/bash --version
/bin/bash --version
At the moment the default bash version is still the outdated one even though “bash --version” may show the you the new version number. When you restart Terminal the old one will still engage as the default. Now we need to whitelist the new directory. For beginners who don’t know how to use vim, nano is best.
sudo nano /etc/shells
Add /usr/local/bin/bash to the top of the list of file paths so it looks similar to this:
/usr/local/bin/bash
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
Since in this tutorial we are going to completely replace the bash file, we don’t need to run the chch command we would normally need to run. chch is the terminal command to change the default shell.
Now is the time to get the new bash to replace the old bash file. First, you need to restart your computer in Reboot Mode to change some permissions. After you shut down your computer, hold down Command+R as the computer boots up again. After that’s done, go to Utilities in the menu bar, then Terminal. Type the following commands:
csrutil enable
reboot
After “reboot” is typed, you should be brought back into your computer normally again. Log in and open Terminal as you usually do. Type these commands listed below. The first one mounts the computer’s own hard drive as entirely read-write (the file we want to change is in a read-only directory). The second command renames the current (old) bash to another name so we can free up that name space. We need to use that filename specifically so that to anyone else who uses bash, it won’t affect them whatsoever. The last command copies the new bash file to the original bash path so now that one will be used.
sudo mount -uw /
sudo mv /bin/bash /bin/bash_old
sudo cp /usr/local/bin/bash /bin/bash
After this is done, everything should work. The last step is to disable csrutil again since it can be dangerous to leave it on. To do this, we need to go back into Reboot Mode. After you shut down your computer, hold down Command+R as the computer boots up again.
After that’s done, go to Utilities in the menu bar, then Terminal. Type the following commands:
csrutil disable
reboot
Congratulations, you have successfully updated your bash shell to the latest version!