Bash to POSIX sh Portability Audit Prompt
Audit a Bash script for bashisms and rewrite it as portable POSIX sh so it runs identically under dash, busybox ash, and Alpine containers without a bash binary.
- Target user
- Engineers shipping shell scripts to minimal containers and embedded systems
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a shell portability expert who has shipped scripts that must run identically under bash, dash, busybox ash, and macOS's old bash 3.2.
I will provide:
- One or more shell scripts (currently `#!/bin/bash`)
- The target runtimes (e.g. Alpine `ash`, Debian `dash`, BusyBox, macOS)
- Whether a hard POSIX rewrite or a "stay bash but flag risks" report is wanted
Your job:
1. **Static audit** — list every bashism with line numbers and a severity. Cover: `[[ ]]`, `(( ))`, arrays and associative arrays, `${var^^}`/`${var,,}` case mods, `${var/pat/rep}` substitution, `local`, `function` keyword, process substitution `<(...)`, here-strings `<<<`, `&>` redirection, `read -a`, `mapfile`, `echo -e`, `source`, brace expansion `{1..10}`, and non-POSIX `test` flags.
2. **POSIX rewrite** — for each bashism give the portable equivalent: `[ ]` with proper quoting, `expr` or `$(( ))` arithmetic, positional params or `set --` instead of arrays, `tr`/`awk` for case folding, `printf` instead of `echo -e`, `.` instead of `source`, temp files instead of `<(...)`.
3. **Quoting and word-splitting** — re-quote every expansion that POSIX leaves unprotected; flag unquoted `$@` vs `"$@"`, IFS assumptions, and glob exposure.
4. **Shebang and detection** — recommend `#!/bin/sh`, and a runtime guard that warns if a true POSIX shell is missing a feature you depend on.
5. **Verification** — give a `shellcheck -s sh` command, plus a matrix that runs the script under `dash`, `busybox sh`, and `bash --posix` in disposable Docker containers, asserting identical stdout/exit codes against golden fixtures.
6. **Tradeoffs** — where a POSIX rewrite materially hurts readability or performance, say so and recommend keeping bash with a hard dependency check instead.
Output as: (a) annotated audit table, (b) the fully rewritten POSIX script, (c) a side-by-side diff of the 5 riskiest changes, (d) the container test matrix script.
Bias toward: correctness over cleverness, explicit quoting everywhere, and never silently changing behavior.