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.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
-
Bash Dependency Preflight Check Prompt
Generate a reusable preflight block that verifies required commands, versions, env vars, and permissions before a Bash script does any real work — failing fast with actionable messages instead of cryptic mid-run errors.
-
Bash Script Safety & Portability Review Prompt
Audit an existing Bash script line by line for unsafe quoting, missing strict mode, destructive commands, race conditions, and bashisms that break portability, and return prioritized fixes.
-
GNU/BSD Coreutils Portability Shim Prompt
Audit a Bash script for GNU-only coreutils flags that break on macOS/BSD, then add a portable shim layer (feature detection plus fallbacks) so the same script runs identically on Linux and macOS runners.
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.