How to upgrade to bash-5 on Mac M1 /M2

Sisi BM
4 min readAug 31, 2023

--

If you own an Apple M1/M2 laptop, almost guaranteed your default version of bash terminal is version 3.2.57(1)-release (arm64-apple-darwin22) . Apple doesn’t support bash version later than 3.2.57 due to licensing issues. Bash versions later than 3.2.57 are licensed under GPLv3 (GNU General Public License version 3), which basically restricted bash distribution on operating systems that block third party software installation. Therefore, Apple Mac OS X users cannot take full advantage of newest-released bash unless upgrade manually.

🌟 This blog’s purpose is to point out some commonly seen confusions people might have when configuring Homebrew-installed bash on Apple Silicon Mac.

Without further ado, let’s get started!

Since we’re going to use new version of bash as a 3rd party software, the go-to tool is homebrew, a popular 3rd party packages installer for Mac. Check if you already have homebrew on your system and its availability by typing brew doctor in terminal. If everything is set up, you should be able to emulate the following result.

$ brew doctor
Your system is ready to brew.

Next up, type brew install bashin terminal, wait for brew to complete the installation.

Now, very important for Mac users: unlike many google search results suggested, brew does not put binary files of bash (or any other packages you installed via Homebrew) in /usr/local/bin, which is the case for intel-chip Macs. Instead, brew put binary files in /opt/homebrew/bin. We should always also include this path in bash’s environment variable PATH. And here it comes another piece of misleading perception about Mac — common way of configuring bash terminal is through its associated .bashrc file. Some Stack Overflow posts would even suggest you to create one .bashrc manually if you can’t find one already existed in your computer. The suggestion is not necessarily wrong, but by default, Mac OS X uses login shell every time, which reads .bash_profile before launching. This means creating and configuring a .bashrc file , which is executed for non-login shells, would be ignored each time you open up a new bash terminal. Configurations put in .bashrc file would be working for the scenario in which you already logged into a bash instance, you edit .bashrc and source it; the changes you made would be reflected immediately. But after you terminate it and open a new bash terminal, OS X would only read and execute .bash_profile so it is suggested that we play around our configurations in this .bash_profile in our home directory.

It is worth noting that, there’s also a bashrc (not a hidden file this time) file exists in /etc path. In general, /etc contains Unix part of OS X that users should not tamper with. You could also see hints through the screenshot below, taken from /etc/bashrc. On the very first line, it says system-wide .bashrc file.

The safe approach is to always add stuff in home directory’s .bash_profile file, aka, ~/.bash_profile. Because even if you break it, your system-wide .bashrc would remain intact.

After configuring .bash_profile, the last thing to do is to put this brew-installed bash into /etc/shells. We could view /etc/shells as our system’s shell whitelist. Type in the following commands to include bash-5 binary path in and check the list.

sudo sh -c 'echo /opt/homebrew/bin/bash >> /etc/shells'
#check the whole shell list
cat /etc/shells

You use sudo to invoke root user privileges. Flag -c instructs sh to read from standard input. And pipe the output of echo to /etc/shells file. Single quotes here prevent text within it from any bash expansion so don’t type double quotes. Quotes matter!

Lastly, we could finally try our newest bash. Type in command

chsh -s /opt/homebrew/bin

Restart a new terminal. You should now see bash upgrade successfully take effect! Wheeee!

The commands which bash, type bash, echo $SHELL, echo $BASH_VERSION, echo $BASH should all yield the similar result. At the same time, older version of default bash still sits in /bin/bash. To confirm, you could also check if you have bash-5 path set up correctly by switching back to older bash-3. And type in which bash or type bash. These two commands reveal your current bash setup, which should be /opt/homebrew/bin/bash now, regardless of which bash version the terminal is running.

Now we could enjoy all the new features added on bash-5. I’ll also be covering some cool new features I found powerful in bash-5 in upcoming posts.

⏦ — — — ✿✰✿ — — — — ✰✿✰ — ——— — ✿✰✿ — — — — — ✰✿✰ — — — ⏦

If you find this blog useful, please don’t forget to toss a fews claps down below for free to support my creating. Happy bash🧋

--

--

Sisi BM

Be diligent, be truthful, be curious, be humble, be practical, be avid, then be a quant.