Skip to main content

2 posts tagged with "bash"

View all tags

Bash 3.2 + set -u: Which Empty Array Expansions Crash?

Β· 6 min read

The same backup restore-drill script runs on schedule on the production server every day; run it under local regression testing and it dies on one arithmetic line with nothing but unbound variable β€” no status file, no log entry.

Ran into this while building CCLee Server Sentinel β€” monitoring and alerting for Linux servers: performance, availability, security and backup. Alerts arrive as conclusions with next steps, and backups are proven restorable by monthly real-restore drills.

TL;DR​

Under set -u, bash 3.2 (macOS's /bin/bash) and bash 5.x disagree about which expansions of an empty array or an undefined name count as "unset" β€” and only one crash site spans every version. Tested live on bash 3.2.57 and 5.3.20, six combinations:

Expressionbash 3.2 (macOS system)bash 5.x (brew / modern Linux)
Empty-array word expansion ${arr[@]} (quoted or not)crashes with unboundfine
Empty-array arithmetic $(( ${#arr[@]} ))fine (yields 0)fine
Undefined name in arithmetic $(( ${#undef[@]} ))crashescrashes

Two guards, one per site: write word expansions as ${arr[@]+"${arr[@]}"} (covers 3.2's empty-array crash), and wrap arithmetic in $( set +u; echo $(( … )) ) (covers the everywhere-fatal undefined-name crash).

Symptom: dead on the arithmetic line, no status file, no log​

The script runs set -u, and its first real statement counts the drill targets:

# pre-fix shape (same structure as the crash site)
EXPECTED=$(( ${#DRILL_DUMPS[@]} + ${#TAR_FILES[@]} ))

DRILL_DUMPS / TAR_FILES come from a config renderer: a list section that exists renders an array assignment; one that doesn't, renders nothing. The conf used for local regression happened to miss a section, so one of the two variables never existed β€” the script exited right there with unbound variable. No drill status JSON, nothing in the log, and 22 regression assertions failing with zero output β€” which looks exactly like the script-under-test being broken.

The confusing part: the same script produced results on production TencentOS daily. "Fine in production, dead locally" β€” the difference was not the script but the local bash version.

Root cause: bash 3.2 and 5.x distribute the crash sites differently​

macOS ships /bin/bash frozen at 3.2.57 (the 2007 GPLv2 release), while Homebrew and modern Linux distributions carry 5.x. Under set -u (nounset), which expansions count as "unset" differs between generations. All of the following is live terminal output:

$ /bin/bash --version | head -1
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26)

$ /bin/bash -uc 'arr=(); for x in ${arr[@]}; do echo $x; done'
bash: arr[@]: unbound variable

$ /bin/bash -uc 'arr=(); for x in "${arr[@]}"; do echo $x; done' # quoted crashes too
bash: arr[@]: unbound variable

$ /bin/bash -uc 'arr=(); echo $(( ${#arr[@]} ))' # arithmetic length is fine
0

$ /bin/bash -uc 'echo $(( ${#undef[@]} ))' # undefined: arithmetic crashes
bash: undef: unbound variable

$ /opt/homebrew/bin/bash -uc 'arr=(); for x in ${arr[@]}; do echo $x; done; echo done'
done # 5.3: empty-array expansion is fine

$ /opt/homebrew/bin/bash -uc 'echo $(( ${#undef[@]} ))'
bash: undef: unbound variable # undefined: 5.3 crashes the same way

Note the second case: quoting does not exempt it. The common belief that "${arr[@]}" is the safe form does not hold on 3.2 under set -u. Meanwhile the empty-array arithmetic length never crashes on either generation; the only everywhere-fatal site is an undefined name in arithmetic (bash: undef: unbound variable β€” notice the error names undef, not undef[@]).

One wrong turn worth recording: the first suspect was the script's own logic, and changing it fixed nothing. The contrast with "works in production" is what redirected the investigation toward the version difference β€” the same variable reference is "unset" to 3.2 and "empty" to 5.x.

The guards: one convention per crash site​

Word expansions β€” ${arr[@]+"${arr[@]}"} means "expand to the element list only if the array is defined; otherwise expand to nothing". This is the +-test form of parameter expansion: it tests before expanding, so 3.2 never reports unbound. Every array loop in the drill script uses this shape:

for pair in ${DRILL_DUMPS[@]+"${DRILL_DUMPS[@]}"}; do

Arithmetic β€” the ${#arr[@]} length form cannot take the +-test, and the undefined-name crash affects every bash version, so wrap the expression in a subshell that locally drops -u:

EXPECTED=$( set +u; echo $(( ${#DRILL_DUMPS[@]} + ${#TAR_FILES[@]} )) )   # set +u:bash3.2 η©Ίζ•°η»„δΏζŠ€

The set +u applies inside the subshell only β€” the rest of the script keeps strict mode. An undefined name becomes 0 in the arithmetic, which is the correct semantics for counting drill targets; if your expression must distinguish "undefined" from "zero", this guard does not apply and you need an explicit emptiness check.

The same "fine in the terminal, dead in a script" family has another member: grep -P works in the terminal but dies in the script? zsh and bash don't share PATH β€” different root cause (two PATHs for interactive vs. script shells), same symptom family.

Watch out

  • Two stub-environment traps in the same family: on macOS, date lives at /bin/date β€” /usr/bin/date does not exist, so tooling that hardcodes the latter fails to find the command.
  • printf '%d' silently emits 0 for an empty value: tested on bash 3.2.57, printf '%d\n' "" prints 0 and exits 0 with no error at all. When asserting numeric fields like last_ok_epoch or epoch, a 0 may be an empty value swallowed by printf, not a real value β€” check the true source.
  • When regression and production run different bash versions, "the test passed" is not "production behaves this way"; either apply both guards everywhere or align versions first with bash --version.

FAQ​

Why does bash report 'unbound variable' for an empty array?​

Under set -u, bash 3.2 (the /bin/bash shipped with macOS) treats any expansion of an empty array β€” ${arr[@]} or "${arr[@]}" β€” as referencing an unset variable. Bash 4.4 and newer allow it. The ${#arr[@]} length form does not crash; an undefined name in arithmetic does, on every version.

How do I protect a bash script against empty arrays under set -u?​

Two guards, one per crash site. For word expansions use ${arr[@]+"${arr[@]}"} β€” expand only when the array is defined. For arithmetic wrap the expression in $( set +u; echo $(( … )) ), which also covers the undefined-name crash that affects every bash version.

Why does the same script behave differently on macOS and Linux?​

macOS ships /bin/bash 3.2.57 from 2007; Linux distributions ship 4.4+ (commonly 5.x). Bash 4.4 relaxed the empty-array behavior under set -u. Install bash 5.x via Homebrew to get Linux-identical behavior for local testing.

CCLEE

Independent developer, 24 years in e-commerce, focused on grounding AI in real business scenarios.

Work with me

grep -P fails in scripts, works in your shell? PATHs differ

Β· 6 min read

A release script that had run unattended on WSL for months died on its first grep call the day it was moved to a Mac: grep: invalid option -- P. What made it worse was the diagnostic itself β€” typing the very same grep -oP '…' into the terminal passed. Same command, same machine: works in the terminal, fails in the script.

TL;DR​

  1. The error has nothing to do with a typo: your interactive zsh and the bash script resolve two different PATHs. Interactively, grep hits a brew-installed GNU grep (which supports -P); in the script it hits /usr/bin/grep (BSD grep, no -P).
  2. The same family of failures extends beyond grep: sha256sum and other GNU coreutils tools exist only in the interactive PATH too, and fail with command not found one step later.
  3. Three portable fixes: rewrite every grep -oP as an equivalent sed -nE extraction; fall back to shasum -a 256 when sha256sum is missing; replace hardcoded paths with runtime self-detection. The script then runs on any machine, any shell.