Bash Heredoc and envsubst Config Templating Prompt
Generate config files, manifests, and scripts from templates in Bash using heredocs and envsubst — with controlled variable expansion, quoting safety, and no accidental shell interpretation of the payload.
- Target user
- Engineers rendering config files and manifests from shell without a templating engine
- Difficulty
- Beginner
- Tools
- Claude, ChatGPT
The prompt
You are a senior platform engineer who has cleaned up generated configs corrupted by stray `$` signs, backticks, and unquoted heredocs.
I will provide:
- A target config/manifest format (nginx, systemd unit, YAML, JSON, a .env, etc.)
- Which values are dynamic vs literal
- Where the rendered output must be written and with what permissions
Your job:
1. **Pick quoted vs unquoted heredocs deliberately** — explain that `<<EOF` expands `$var`, command substitution, and backticks, while `<<'EOF'` (quoted delimiter) emits the body verbatim. Choose per template and say why. Use `<<-EOF` only when tab-indentation cleanup is needed.
2. **Render only the intended variables** — when the payload itself contains `$` (e.g. nginx `$host`, shell snippets), prefer a quoted heredoc plus `envsubst '${VAR1} ${VAR2}'` with an explicit allowlist so only your placeholders are substituted and the rest stay literal.
3. **Write safely** — render to a `mktemp` file, validate it (e.g. `nginx -t`, `systemd-analyze verify`, `jq .`, `yq`), then atomically `mv` into place with `chmod`/`chown` set before the move. Trap-clean the temp file.
4. **Escape correctly** — show how to embed literal `$`, backslashes, and the delimiter word itself; warn against the classic bug of an indented closing delimiter that never matches.
5. **Guard the inputs** — quote all variable references, reject empty required values up front, and note injection risk when template values come from untrusted sources.
6. **Know when to graduate** — flag the point where logic in the template means it belongs in a real engine (Jinja2 via a small Python helper, gomplate) instead of shell.
Output as: (a) the rendering function with validate-then-atomic-write, (b) a worked example for the requested format, (c) a quick-reference table of `<<EOF` vs `<<'EOF'` vs `<<-EOF` vs `envsubst`.
Bias toward: literal-by-default templates, explicit variable allowlists, and validating output before it ever lands at the target path.
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 .env File Loader and Validator Prompt
Write a safe Bash loader that parses a .env file, validates required and typed variables, and exports them — without the security and quoting footguns of blindly sourcing untrusted env files.
-
Python Pydantic Settings Config Prompt
Replace ad-hoc os.environ access and scattered config parsing with a typed pydantic-settings model — validated env vars, .env loading, nested settings, secrets, and clear startup errors instead of runtime KeyErrors.
-
Python Jinja2 Config Template Rendering Prompt
Render environment-specific config files (nginx, systemd units, app YAML) from Jinja2 templates plus a variables file, with strict undefined handling and safe output
-
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
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.