POSIX sh versus Bash
Portability means a script works under an explicitly chosen contract, not every shell ever made. POSIX specifies a shell language and utilities; sh is an invocation name, not a particular implementation. This chapter uses POSIX.1-2017 (Issue 7) as a conservative baseline and discusses POSIX.1-2024 (Issue 8) separately. Issue 8 standardizes pipefail and dollar-single quoting ($'...' with specified escapes); older implementations may lack them. Bash, dash and BusyBox ash are different interpreters. For a portable script use #!/bin/sh on the intended Unix-like host and test the actual sh; for Bash extensions use an explicit Bash interpreter such as #!/usr/bin/env bash when PATH is controlled. Invoking sh script bypasses the intended interpreter selection of a Bash shebang.
Use [ "$a" = "$b" ], case, name() { ...; }, $((...)), $(...), . ./helpers.sh and IFS= read -r line for the older common baseline. [[ ]], indexed and associative arrays, ((...)) as a command, source, brace expansion, process substitution and read -a are not guaranteed by that baseline or Issue 8. local is widely implemented but not a POSIX shell builtin. Arithmetic expansion is different from the arithmetic command. Use positional parameters for a small list, case for patterns, and explicit status checks in place of unsupported pipeline options. bash --posix changes selected behaviors; it does not reject all Bash extensions. The matrix later in this chapter tests this distinction. Dot loading must succeed before the caller uses its variables. The portable examples are checked on actual local interpreters; the Bash-only array example is not part of that portability claim.
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.
Portable list and test
set -- 'daily report' backupfor item do if [ "$item" = 'daily report' ]; then printf 'found: %s\n' "$item" fidoneExpected stdout:
found: daily reportQuoted positional parameters preserve the first value as one word.
Bash array in POSIX mode
bash --posix -c 'items=("daily report" backup); printf "%s\n" "${items[0]}"' Expected stdout:
daily reportThe array still works in this Bash mode; successful parsing is not proof of portable syntax.
Portable dot command
printf '%s\n' 'label=ready' > helper.sh || exit 1. ./helper.sh || exit 1printf '%s\n' "$label"Expected stdout:
readyDot executes the trusted file in the current shell; an explicit relative path avoids PATH lookup.
Common mistakes
Changing only the shebang does not make arrays portable. Quote operands to [, use = rather than ==, and test the actual interpreter; Bash POSIX mode is not a strict syntax validator.
Practice
Rewrite a Bash array of two values (one containing spaces) using positional parameters. Verify output in Bash, dash and BusyBox ash; explain why this does not prove complete POSIX conformance.
Next: Bash version compatibility