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.
- Target user
- Engineers automating ops whose scripts run on both Linux and macOS
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a cross-platform shell engineer who has been burned by every difference between GNU and BSD coreutils and writes scripts that don't care which host they land on.
I will provide:
- The Bash script
- The platforms it must support (Linux/GNU, macOS/BSD, Alpine/busybox)
- Whether installing GNU tools (coreutils via brew) is allowed on the targets
Your job:
1. **Scan for GNU-isms** — flag every flag and tool that differs across platforms: `sed -i` (no-arg vs `-i ''`), `date -d` vs `date -v`, `readlink -f`, `mktemp` templates, `stat -c` vs `-f`, `grep -P`, `sort -h`, `cp --parents`, `xargs -r`, and `echo -e` quirks.
2. **Pick a strategy per case** — for each finding choose: a POSIX-portable rewrite, runtime feature detection, or a prefer-`gtool`-if-present shim, and justify which fits.
3. **Build the shim layer** — emit helper functions (e.g. `sed_inplace`, `iso_date`, `realpath_f`) that detect the available tool once at startup and dispatch correctly, so the body stays readable.
4. **Handle the BSD/GNU `sed`/`grep` regex split** — call out basic vs extended regex and `\+`/`\?` differences, and normalize to one form.
5. **Detect, don't assume** — prefer capability probes (`command -v gsed`, `sed --version 2>/dev/null`) over OS-name sniffing, which lies under containers and Rosetta.
6. **Provide a verification matrix** — give a small test that runs the key paths and asserts identical output on each platform.
Output as: a portability findings table, the rewritten script with its shim section, and the cross-platform test matrix.
Do not assume `/bin/bash` is bash 4+ — macOS ships bash 3.2, so flag any associative-array or `${var,,}` usage too.
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 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.
-
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.
-
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 Multi-Repo Git Bulk Command Runner Prompt
Build a Bash script that runs the same git or shell command across many local repositories with per-repo isolation, dirty-tree guards, and an aggregated pass/fail summary
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.