Checking Your Bash Version

Su Suriya Ravichandran Updated 06 Dec 2026
6 min read ·Lesson 10 of 205

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:

IndexMeaningExample
0Major version5
1Minor version2
2Patch level21
3Build version1
4Release statusrelease
5Machine typex86_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:

  1. The (( ... )) form evaluates an arithmetic condition. Inside it, BASH_VERSINFO[0] is read as a number without needing a dollar sign.
  2. If the major version is below 4, the condition is true and the if block runs.
  3. The message is sent to standard error with >&2, so it does not mix with normal output.
  4. exit 1 ends 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:

FeatureAvailable from
Indexed arraysBash 2.0
[[ ... ]] conditionalBash 2.02
Associative arrays (declare -A)Bash 4.0
mapfile / readarrayBash 4.0
Case-modification expansions ${var^^} and ${var,,}Bash 4.0
wait -nBash 4.3
${var@Q} and similar transformationsBash 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.

0 Comments

Reviewed before they appear

No comments yet.

Bash
Introduction to Bash What Is a Shell? What Is Bash (Bourne Again Shell)? Bash vs. Shell vs. Terminal Bash vs. sh, Zsh, and Fish Where Bash Is Used: Linux, DevOps, and Automation Installing Bash on Linux Using Bash on macOS Using Bash on Windows with WSL Checking Your Bash Version Opening and Using a Terminal Running Your First Bash Command Understanding Command Syntax and Arguments Bash Built-in Commands vs. External Commands Working with pwd, ls, and cd Creating Files and Directories with touch and mkdir Copying, Moving, and Renaming Files Removing Files and Directories Safely Viewing Files with cat, less, head, and tail Searching for Files with find Searching Text with grep Understanding File Paths and Wildcards Using man Pages and Command Help Bash Script Structure Creating Your First Bash Script Understanding the Shebang (#!/usr/bin/env bash) Making a Script Executable with chmod Running a Bash Script in Different Ways Writing Comments in Bash Using echo and printf Reading User Input with read Displaying Helpful Messages in Scripts Using Semicolons and Newlines Bash Variables: Declaration and Usage Rules for Naming Bash Variables Environment Variables Local Variables vs. Environment Variables Exporting Variables with export Common Environment Variables: HOME, PATH, USER, and PWD Read-Only Variables with readonly Removing Variables with unset Command-Line Arguments: $0, $1, and More Special Parameters: $#, $?, $$, $!, $@, and $* Positional Parameters and shift Default Values with Parameter Expansion Single Quotes vs. Double Quotes Escaping Special Characters Command Substitution with $(...) Arithmetic Expansion with $((...)) Brace Expansion Tilde Expansion and Home Directories Filename Expansion and Globbing Understanding Word Splitting Parameter Expansion Basics Why Quoting Variables Matters Standard Input, Standard Output, and Standard Error Redirecting Output with > and >> Redirecting Input with < Redirecting Errors with 2> Combining Standard Output and Standard Error Using Pipes (|) to Connect Commands Using tee to Save and Display Output Here Documents with << Here Strings with <<< Command Lists with ;, &&, and || Conditional Statements with if, then, and fi Using elif and else Testing Conditions with test and [ ] Using [[ ]] for Bash Conditionals Comparing Strings in Bash Checking Whether Files Exist Checking File Types and Permissions Integer Comparisons in Bash Logical Operators: &&, ||, and ! Using case Statements Pattern Matching in case Common Conditional Statement Mistakes for Loops in Bash C-Style for Loops while Loops until Loops Looping Through Files and Directories Looping Through Command Output Safely break and continue Nested Loops select Loops for Simple Menus Avoiding Infinite Loops Defining and Calling Bash Functions Passing Arguments to Functions Returning Status Codes from Functions Understanding return vs. echo in Functions Local Variables in Functions Function Scope and Variable Visibility Organizing Scripts into Functions Sourcing Other Bash Files with source Reusable Bash Utility Functions Indexed Arrays in Bash Creating and Accessing Array Elements Adding and Removing Array Elements Looping Through Arrays Array Length and Indices Associative Arrays in Bash Iterating Over Associative Arrays Passing Arrays to Functions Common Array Pitfalls Processing Text with grep, sed, and awk Using cut, sort, uniq, and wc Finding and Replacing Text with sed Extracting Columns with awk Parsing CSV-Like Text from the Command Line Working with Filenames That Contain Spaces Safe File Handling with find -print0 and xargs -0 Temporary Files with mktemp Reading Files Line by Line Appending Logs to a File Understanding Exit Codes Checking Command Success with $? Using exit to End a Script Fail-Fast Scripts with set -e: Benefits and Caveats Using set -u and set -o pipefail Combining Strict Mode Options Safely Handling Errors with if Statements Traps with trap Cleaning Up Temporary Files on Exit Writing Useful Error Messages Debugging Scripts with bash -x Parsing Script Arguments Validating Required Arguments Handling Optional Arguments Parsing Options with getopts Building Help Messages with --help Setting Defaults for Optional Parameters Handling Unknown Options and Invalid Input Creating Interactive Menus in Bash Foreground and Background Processes Running Commands in the Background with & Managing Jobs with jobs, fg, and bg Waiting for Processes with wait Capturing Process IDs with $! and $$ Sending Signals with kill Handling Interrupts and Termination Signals Running Commands with timeout Basic Process Monitoring from Bash Aliases and Shell Functions: When to Use Each Customizing the Bash Prompt (PS1) Bash Startup Files: .bashrc, .bash_profile, and Profile Interactive vs. Non-Interactive Bash Shell Options with set shopt: Enabling Bash-Specific Options Directory Stack with pushd, popd, and dirs Process Substitution with <(...) and >(...) Advanced Parameter Expansion Pattern Matching and Extended Globbing Bash Script Security Fundamentals Avoiding Command Injection in Shell Scripts Quoting User Input Safely Why eval Is Risky and Safer Alternatives Handling Secrets and Credentials in Scripts Using Least Privilege in Automation Scripts Safe Use of rm and Destructive Commands Validating Paths and User-Supplied Filenames ShellCheck: Static Analysis for Bash Scripts Writing Readable and Maintainable Bash Automating Backups with Bash Creating Log Rotation and Cleanup Scripts Checking Disk Usage and Free Space Monitoring CPU, Memory, and System Load Checking Website Availability with curl Automating Updates on Ubuntu and Debian Scheduling Bash Scripts with cron Using Lock Files to Prevent Duplicate Runs Building a Simple Health-Check Script Sending Notifications from Bash Scripts Using Bash in Git Workflows Bash Scripts for Docker Automation Automating Docker Container Health Checks Using Bash in CI/CD Pipelines Environment Configuration for Deployment Scripts Automating SSH Tasks with Bash Deploying a Web Application with a Bash Script Using Bash to Process Application Logs Building Repeatable Development Setup Scripts Bash Debugging Techniques Using set -x and PS4 for Debug Traces Testing Bash Scripts with Bats Mocking Commands During Script Tests Testing Error Paths and Exit Codes Writing Portable Shell Scripts Bash-Specific Syntax vs. POSIX Shell Syntax Using ShellCheck in a Development Workflow Performance Tips for Bash Scripts Build an Interactive Calculator Build a File and Directory Organizer Build a Timestamped Backup Script Build a Log File Analyzer Build a Bulk File Renaming Tool Build a System Resource Monitoring Script Build a Website Uptime Checker Build a User Account Audit Report Build a Database Backup Automation Script Build a Docker Cleanup Helper Build a Server Health-Check and Alert Script Build a Deployment Automation Script Build a Menu-Driven Linux Administration Toolkit Build a Safe Automated Backup and Restore Workflow
Ask about this post
AI Ask about this post

Ask questions about Checking Your Bash Version and get answers drawn from it.

Signed-in readers only.