Bash Strict Mode Script Scaffold Prompt
Generate a production-grade Bash script skeleton with strict mode, trap-based cleanup, structured logging, and argument parsing so every new script starts hardened instead of being retrofitted later.
- Target user
- DevOps engineers who keep copy-pasting fragile shell scripts
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior shell engineer who treats Bash like a real programming language and refuses to ship scripts that fail silently.
I will provide:
- The script's purpose (one paragraph)
- Target shell + minimum version (e.g. bash 4.4, or POSIX sh)
- Required inputs/flags and any required external commands
- Where it runs (cron, CI, systemd, an operator's laptop)
Produce a complete, runnable scaffold and explain each decision:
1. **Shebang + strict mode** — `#!/usr/bin/env bash`, then `set -Eeuo pipefail` and `IFS=$'\n\t'`. Explain exactly what each flag prevents and the one common case where `-e` surprises people (so they don't blindly disable it).
2. **Self-locating + safe globals** — `SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"`, `readonly` constants, and a top-of-file config block.
3. **Logging** — `log()`, `warn()`, `die()` helpers that write to stderr with timestamps and severity; respect a `--quiet`/`--verbose` flag and a `NO_COLOR` env var.
4. **Argument parsing** — a `while`/`case` getopts-style loop supporting `-h/--help`, `--dry-run`, long options, and a `usage()` heredoc. Validate required args and exit non-zero with a clear message when missing.
5. **Dependency checks** — a `require_cmd` function that verifies every external binary up front and dies with an actionable message naming the missing tool.
6. **Cleanup via trap** — `trap cleanup EXIT INT TERM`, a `cleanup()` that removes temp files created with `mktemp`, and an `ERR` trap that prints the failing line number and command.
7. **The actual work** — wrap it in a `main()` function called as `main "$@"` at the bottom so sourcing the file for tests runs nothing.
8. **Idempotency + dry-run** — show how `--dry-run` short-circuits mutating commands via a `run()` wrapper that echoes instead of executes.
Output: (a) the full annotated script, (b) a shellcheck-clean note listing any directives needed and why, (c) a 5-line "how to test it" snippet.
Bias toward: fail loud and early, no unquoted expansions, no silent overwrites.
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 Error Trap and Self-Diagnosing Script Prompt
Add an ERR trap, line-number-aware error reporting, and an opt-in xtrace debug mode to a Bash script so failures print exactly where and why they died instead of a silent non-zero exit.
-
Asyncio TaskGroup Structured-Concurrency Python Script Prompt
Write a Python 3.11+ automation script that fans work out with asyncio.TaskGroup so failures cancel siblings cleanly, errors aggregate into an ExceptionGroup, and no task is ever silently orphaned.
-
Bash Hung-Process Guard with timeout Prompt
Wrap flaky external commands (curl, ssh, database clients, cloud CLIs) in a Bash timeout/kill guard so a single hung call can never stall a cron job, CI step, or systemd task forever.
-
Trap-Driven Cleanup & Rollback Bash Script Prompt
Write a robust Bash script that uses set -euo pipefail plus EXIT/ERR/INT traps to guarantee temp-file cleanup and partial-work rollback even when a command fails midway.
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.