Using Bash on macOS
macOS is built on a Unix foundation, so Bash is available in every Mac. However, Apple ships a very old Bash version and has used Zsh as the default login shell since macOS Catalina (10.15). This lesson explains what you get out of the box, how to open Bash, how to install a modern version with Homebrew, and how to make your scripts pick the right one.
Understanding the Concept
Three facts explain most of the surprises people meet on a Mac:
- The built-in Bash is version 3.2. Apple has kept it at 3.2 for many years because later versions are released under the GNU General Public License version 3, while 3.2 uses version 2. The built-in copy is located at
/bin/bash. - The default interactive shell is Zsh. New user accounts open Zsh in the Terminal app. Bash still exists, but you must ask for it.
- A newer Bash can be installed alongside the old one. Package managers such as Homebrew install the current release in a separate location, leaving the system copy untouched.
The old version matters because many features in this tutorial, such as associative arrays and some newer expansions, need Bash 4 or later. Bash 3.2 does not have them.
Opening Terminal and Checking Your Shell
Open the Terminal app (found in Applications, then Utilities, or through Spotlight search). To find out which shell your session runs, use:
echo "$SHELL"
On a recent Mac the output is normally:
/bin/zsh
SHELL shows your configured login shell. To see which shell is actually running in this window:
ps -p $$ -o comm=
This prints the name of the current shell process, for example zsh.
Using the Built-In Bash
To start the system Bash from the Terminal, type:
bash
A new Bash session starts inside your current window. Check its version:
echo "$BASH_VERSION"
On a stock Mac the output looks like:
3.2.57(1)-release
The exact patch number may differ, but the major and minor numbers will be 3.2. Type exit to return to Zsh.
Running a script with the system Bash is the same as on any other system:
bash myscript.sh
The script runs under Bash 3.2, so it must avoid features introduced later.
Installing a Modern Bash with Homebrew
Homebrew is a widely used package manager for macOS. After installing Homebrew by following the instructions on its official website, install Bash with:
brew install bash
Homebrew places the new program under its own directory instead of replacing /bin/bash:
- On Apple Silicon Macs, the Homebrew prefix is
/opt/homebrew, so the new Bash is at/opt/homebrew/bin/bash. - On Intel Macs, the prefix is
/usr/local, so it is at/usr/local/bin/bash.
Confirm which Bash each path runs:
/bin/bash --version | head -1
/opt/homebrew/bin/bash --version | head -1
The first line reports version 3.2.x and the second reports a current 5.x release. Use the path that matches your Mac's processor.
Check which one your plain bash command finds:
type -a bash
The output lists every bash found on your PATH, in order. The first entry is the one that runs when you type bash. If Homebrew's directory comes before /bin in your PATH, you get the new version.
Making Scripts Use the Right Bash
A script chooses its interpreter with the shebang line. The portable form is:
#!/usr/bin/env bash
The env program searches PATH for bash, so the script uses whichever Bash comes first. On a Mac with Homebrew in PATH, that is the modern version. In contrast, #!/bin/bash always selects Apple's old Bash 3.2.
A script can also protect itself by checking the version at the start:
#!/usr/bin/env bash
if (( BASH_VERSINFO[0] < 4 )); then
echo "This script needs Bash 4 or newer. Found: $BASH_VERSION" >&2
exit 1
fi
echo "Bash $BASH_VERSION is new enough"
Run on Bash 5.2, it prints:
Bash 5.2.21(1)-release is new enough
Run on Bash 3.2, it prints the error message to standard error and exits with status 1. The (( ... )) form evaluates an arithmetic comparison, and >&2 sends the message to the error stream so it is not mixed with normal output.
Making Bash Your Login Shell
If you prefer Bash for interactive use, you can change the login shell. First add the Bash you want to the list of allowed shells. For a Homebrew Bash on Apple Silicon:
echo /opt/homebrew/bin/bash | sudo tee -a /etc/shells
The command appends the path to /etc/shells. The tee -a form is used because a plain >> redirection would run without administrator rights. Then switch:
chsh -s /opt/homebrew/bin/bash
Open a new Terminal window for the change to take effect. For the system Bash, use chsh -s /bin/bash instead, which is already allowed. You may also see a notice from Apple that the default shell is now Zsh and that the Bash you are using is old. That message is informational.
Start-Up Files on macOS
The Terminal app starts each new window as a login shell, which affects which configuration files Bash reads. A login Bash reads ~/.bash_profile first, and only falls back to ~/.bash_login and then ~/.profile when it is missing. It does not read ~/.bashrc automatically. A common arrangement is to load ~/.bashrc from ~/.bash_profile:
# ~/.bash_profile
if [ -f "$HOME/.bashrc" ]; then
source "$HOME/.bashrc"
fi
The condition checks that the file exists, and source runs its commands in the current shell. A later lesson covers all the start-up files in detail.
Differences from Linux Commands
Many everyday commands on macOS come from BSD, whereas Linux uses GNU versions. The same name can accept different options. For instance, sed -i on GNU systems edits files in place with no extra argument, while the BSD version on macOS needs a backup suffix argument, such as sed -i ''. Likewise, options of date, stat, and ls differ.
You can install the GNU versions with Homebrew, for example brew install coreutils, and they appear with a g prefix such as gdate. When writing scripts that must run on both Linux and macOS, test them on both, or restrict yourself to options that both versions share.
macOS Defaults at a Glance
| Item | macOS default |
|---|---|
| Default login shell | Zsh (since Catalina) |
| Built-in Bash | Version 3.2 at /bin/bash |
| Modern Bash source | Homebrew |
| Homebrew Bash path | /opt/homebrew/bin/bash (Apple Silicon) or /usr/local/bin/bash (Intel) |
| Login-shell start-up file | ~/.bash_profile |
| Core utilities | BSD versions |