How to get Bash version number in Mac

macOS ships with Bash, but the version Apple includes is ancient — Bash 3.2 from 2007. If your scripts rely on features introduced in Bash 4 or later (associative arrays, mapfile, globstar), you need to know what you're running and how to upgrade. Here are all the ways to check.


Method 1: bash --version

The most direct way — run the bash binary with the --version flag:

bash --version

On a stock macOS installation you'll see:

GNU bash, version 3.2.57(1)-release (arm64-apple-darwin23)
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>

If you've installed a newer version via Homebrew, the output will reflect that instead (e.g., 5.2.26(1)-release).


Method 2: $BASH_VERSION Variable

Inside any running Bash session, the $BASH_VERSION variable always holds the current shell's version:

echo $BASH_VERSION
# 3.2.57(1)-release   (Apple's built-in)
# 5.2.26(1)-release   (Homebrew-installed)

This is useful inside scripts when you want to check the runtime version programmatically:

#!/bin/bash
if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then
    echo "This script requires Bash 4 or later. You have: $BASH_VERSION" >&2
    exit 1
fi
echo "Good — running Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]}"

$BASH_VERSINFO is an array: [0] is the major version, [1] is the minor version.


Method 3: which bash and file Path Check

To see which bash binary is actually on your PATH:

which bash
# /bin/bash          (Apple's system bash)
# /opt/homebrew/bin/bash   (Homebrew bash on Apple Silicon)
# /usr/local/bin/bash      (Homebrew bash on Intel)

If which bash shows /bin/bash but you installed Homebrew's bash, your PATH may not be putting /opt/homebrew/bin first. Check with echo $PATH.


Why Does macOS Ship Bash 3.2?

This comes down to licensing. Bash 3.2 is the last version released under GPLv2. Starting with Bash 4.0, the project moved to GPLv3. Apple's legal policy prevents them from shipping GPLv3 software in macOS, so they're stuck distributing the 2007 Bash 3.2 binary.

This is also why Apple switched the default interactive shell to Zsh in macOS Catalina (10.15, 2019). Zsh uses the MIT license — no GPL restrictions. If you open Terminal on a fresh Mac, you're already using Zsh, not Bash.


Bash 3.2 vs Bash 5: What's Missing

FeatureBash 3.2Bash 4+
Associative arrays (declare -A)Not availableBash 4.0+
mapfile / readarrayNot availableBash 4.0+
** globstar (recursive glob)Not availableBash 4.0+ with shopt -s globstar
read -i (default value in read)Not availableBash 4.0+
Case-insensitive matchingLimitedBash 4.0+ with shopt -s nocasematch
Improved string manipulationBasicBash 4.0+ adds ${var^^}, ${var,,}
Nameref variables (declare -n)Not availableBash 4.3+

Installing a Modern Bash with Homebrew

To get Bash 5 on macOS:

brew install bash

Homebrew installs it at /opt/homebrew/bin/bash (Apple Silicon) or /usr/local/bin/bash (Intel). Verify:

/opt/homebrew/bin/bash --version
# GNU bash, version 5.2.26(1)-release

Making Homebrew Bash the Default

To use it as your default login shell, first add it to the list of approved shells, then change your shell:

# Add to /etc/shells (requires sudo)
echo "/opt/homebrew/bin/bash" | sudo tee -a /etc/shells

# Change your login shell
chsh -s /opt/homebrew/bin/bash

Open a new terminal session and verify:

echo $BASH_VERSION
# 5.2.26(1)-release

Note: Even after this, /bin/bash still exists and is still Bash 3.2. Scripts with #!/bin/bash shebangs continue to use the old version. Use #!/usr/bin/env bash to pick up whichever bash is first on your PATH.


Should You Upgrade or Switch to Zsh?

Since macOS Catalina, Zsh is Apple's recommended default shell for interactive use. Zsh has feature parity with modern Bash and adds improvements like better tab completion, array handling, and the Oh My Zsh ecosystem. If you're writing shell scripts for portability across macOS and Linux, consider:

  • Use Zsh (#!/bin/zsh) for macOS-specific scripts
  • Use POSIX sh (#!/bin/sh) for maximum portability
  • Use Bash 5 from Homebrew (#!/usr/bin/env bash) for scripts that need Bash-specific Bash 4+ features on macOS

Summary

Check the Bash version on macOS with bash --version or by reading $BASH_VERSION inside a script. macOS ships Bash 3.2 (from 2007) because Apple cannot distribute newer Bash versions due to the GPLv3 license. Install Bash 5 via brew install bash for access to associative arrays, globstar, mapfile, and other modern features. macOS Catalina and later default to Zsh; use #!/usr/bin/env bash in scripts so they pick up whichever bash is first on the PATH.

No comments :

Post a Comment

Please leave your message queries or suggetions.

Note: Only a member of this blog may post a comment.