Bash version compatibility
Choose a minimum Bash version based on the features you need and the versions deployed, then test on actual interpreters. BASH_VERSION is descriptive text; BASH_VERSINFO contains major, minor and patch numbers at indices 0, 1 and 2. Compare numeric components, not version strings (5.10 is newer than 5.2). Associative arrays and mapfile arrived in Bash 4.0; namerefs and wait -n in 4.3. Bash 3.2 cannot use these features. A guard in a script must itself parse on the oldest supported interpreter; put newer incompatible syntax in a separate file if necessary. A non-Bash shell cannot necessarily parse even the guard. Use trusted literal variable names for nameref examples; never treat arbitrary input as a variable name. BASH_COMPAT (introduced in 4.3) selects certain historical semantics, not an earlier binary, parser or full feature set. Test exact version and behavior; a package version may include distribution patches. If only older Bash is supported, a pair of case branches can replace a small associative lookup, and explicit output can replace a nameref in some designs. A version gate checks the running interpreter, not every dependency. Select and test the minimum version on the target host; compatibility mode does not replace that test.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Compare numeric version components
major=${BASH_VERSINFO[0]}minor=${BASH_VERSINFO[1]}if (( major > 4 || (major == 4 && minor >= 3) )); then printf 'version gate passed\n'else printf 'requires Bash 4.3+\n' >&2 exit 2fiExpected stdout:
version gate passedThe installed 5.2 interpreter passes the numeric gate. This is not a test of older releases.
Compatibility level is not a binary
before=$BASH_VERSIONBASH_COMPAT=4.2 || exit 1printf 'level=%s\n' "$BASH_COMPAT"[ "$before" = "$BASH_VERSION" ] || exit 1printf 'same interpreter\n'Expected stdout:
level=4.2same interpreterThe version string remains unchanged after selecting a compatibility level.
Fallback for a small lookup
lookup() { case $1 in dev) printf '8080\n' ;; prod) printf '443\n' ;; *) return 2 ;; esac}lookup prodExpected stdout:
443The case form needs neither arrays nor namerefs.
Common mistakes
Do not compare version strings lexically or claim BASH_COMPAT=3.2 tests Bash 3.2. A version guard cannot rescue syntax the interpreter cannot parse.
Practice
Write a Bash version check for 4.3, a case-based fallback for a two-key lookup, and a test plan for real Bash 3.2, 4.3 and 5.x. Mark unavailable versions NOT RUN.
Next: Utility dialects and platform assumptions