Checking Your Bash Version
Different Bash versions support different features, so knowing which version you are running is the first step when a command or script behaves unexpectedly. This lesson shows several ways to check the version from the command line, how to read the version number, how to inspect it inside a script, and how to make a script stop politely when the version is too old.
Understanding the Concept
A Bash version number has the form major.minor.patch, followed by a build number and a release status, for example 5.2.21(1)-release.
- Major (
5): a large generation of Bash. New major versions add significant features. - Minor (
2): smaller feature additions within a major generation. - Patch (
21): bug fixes and security corrections. - Build (
1) and status (release): details about how this particular build was made.
Features appear in specific versions. For instance, associative arrays need Bash 4.0 or later, which is the reason a script that works on a modern Linux machine can fail on a Mac that still carries Bash 3.2.
Checking from the Command Line
Using the --version Option
The simplest check asks the Bash program to describe itself:
bash --version
The output contains several lines, starting with the version and followed by a licence notice. To keep only the first line:
bash --version | head -1
Sample output:
GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
The text in parentheses at the end names the machine type the build was made for. Note that this command reports the Bash program found on your PATH. It does not necessarily describe the shell that is running your current session.
Using the BASH_VERSION Variable
Every running Bash shell stores its own version in the variable BASH_VERSION:
echo "$BASH_VERSION"
Sample output:
5.2.21(1)-release
This reports the version of the shell currently executing your commands. If the variable prints nothing, you are not running Bash at all, which can happen when you are in Zsh or another shell.
Using the BASH_VERSINFO Array
The array BASH_VERSINFO splits the version into separate elements, which makes comparisons easy. Display all of them:
printf '%s\n' "${BASH_VERSINFO[@]}"
Output:
5
2
21
1
release
x86_64-pc-linux-gnu
The six elements, by index, are:
| Index | Meaning | Example |
|---|---|---|
| 0 | Major version | 5 |
| 1 | Minor version | 2 |
| 2 | Patch level | 21 |
| 3 | Build version | 1 |
| 4 | Release status | release |
| 5 | Machine type | x86_64-pc-linux-gnu |
Because the numbers sit in separate elements, a script can compare them directly without parsing a string. The expression ${BASH_VERSINFO[0]} gives the major version.
Telling Which Bash Is Running
Two Bash programs can exist on one machine, for example the system copy and one installed by a package manager. Two commands help you tell them apart:
type -a bash
echo "$BASH"
Sample output:
bash is /usr/bin/bash
bash is /bin/bash
/bin/bash
type -a bash lists every bash found on PATH, in search order. The variable BASH contains the full path of the program that is running the current shell. If a script prints a different version than bash --version does, comparing these paths usually explains why.
A Script That Reports the Version
The following script prints each part of the version in a readable format:
#!/usr/bin/env bash
echo "Full version : $BASH_VERSION"
echo "Major : ${BASH_VERSINFO[0]}"
echo "Minor : ${BASH_VERSINFO[1]}"
echo "Patch : ${BASH_VERSINFO[2]}"
echo "Machine type : ${BASH_VERSINFO[5]}"
echo "Bash path : $BASH"
Output:
Full version : 5.2.21(1)-release
Major : 5
Minor : 2
Patch : 21
Machine type : x86_64-pc-linux-gnu
Bash path : /usr/bin/bash
Each echo line reads one array element or variable. The padding spaces align the colons so that the report is easy to scan.
Requiring a Minimum Version
A script that uses newer features should check the version first and exit with a clear message if it is too old. This is better than letting the script fail halfway through with a confusing syntax error:
#!/usr/bin/env bash
if (( BASH_VERSINFO[0] < 4 )); then
echo "Bash 4 or newer is required. Found: $BASH_VERSION" >&2
exit 1
fi
echo "Bash $BASH_VERSION is new enough"
On Bash 5.2 the output is:
Bash 5.2.21(1)-release is new enough
How it works:
- The
(( ... ))form evaluates an arithmetic condition. Inside it,BASH_VERSINFO[0]is read as a number without needing a dollar sign. - If the major version is below 4, the condition is true and the
ifblock runs. - The message is sent to standard error with
>&2, so it does not mix with normal output. exit 1ends the script with a failure status that callers can detect.
Comparing Major and Minor Together
Sometimes a feature needs a specific minor version, such as 4.4. A reusable function can compare both numbers:
version_at_least() {
local want_major=$1 want_minor=${2:-0}
(( BASH_VERSINFO[0] > want_major ||
(BASH_VERSINFO[0] == want_major && BASH_VERSINFO[1] >= want_minor) ))
}
version_at_least 4 4 && echo "4.4 or newer: yes"
version_at_least 6 0 || echo "6.0 or newer: no"
Output on Bash 5.2:
4.4 or newer: yes
6.0 or newer: no
The function succeeds when the running major version is greater than the requested one, or when the major versions are equal and the minor version is at least the requested minor. The expression ${2:-0} means "use the second argument, or 0 if it was not given". Because the function's exit status comes from the arithmetic test, you can use it directly with && and ||.
Checking a Different Shell
BASH_VERSION and BASH_VERSINFO exist only in Bash, which makes them a handy test for whether a script is really running under Bash:
sh -c 'echo "[${BASH_VERSION:-not bash}]"'
Output when sh is Dash:
[not bash]
The expansion ${BASH_VERSION:-not bash} substitutes the text not bash when the variable is empty or unset. A script can use the same idea to refuse to run under the wrong shell.
To check another installed program, use its own option. Many shells support --version, although Dash does not, and some print their version only through variables, such as ZSH_VERSION in Zsh.
Checking the Package Version
Your distribution's package manager also records the installed Bash package. On Debian-based systems:
apt-cache policy bash | head -3
Sample output:
bash:
Installed: 5.2.21-2ubuntu4
Candidate: 5.2.21-2ubuntu4
Installed is the version on your machine, and Candidate is the newest version the configured repositories offer. When the two differ, an upgrade is available.
Version Differences That Matter
A short list of features and the Bash release that introduced them helps you decide how new your minimum should be:
| Feature | Available from |
|---|---|
| Indexed arrays | Bash 2.0 |
[[ ... ]] conditional | Bash 2.02 |
Associative arrays (declare -A) | Bash 4.0 |
mapfile / readarray | Bash 4.0 |
Case-modification expansions ${var^^} and ${var,,} | Bash 4.0 |
wait -n | Bash 4.3 |
${var@Q} and similar transformations | Bash 4.4 |
Treat this table as a guide and confirm details in the GNU Bash manual for the version you target. A quick test of any feature on the oldest system you support is the most reliable check:
bash -c 'declare -A m; m[a]=1; echo "${m[a]}"'
Output on Bash 4 or later:
1
On Bash 3.2, the same command fails with an error such as declare: -A: invalid option, which tells you immediately that associative arrays are not available.