| name | bash-script-generator |
| type | standard |
| depth | extended |
| description | Generates production-ready bash 5.2+/5.3 scripts with strict mode, immutable locals, dispatch tables, and functional patterns. Use when creating new .sh scripts, CLI tools, cron jobs, deployment automation, text processing workflows, or log analyzers. |
[H1][BASH-SCRIPT-GENERATOR]
Dictum: Functional patterns and strict mode produce maintainable shell automation.
Generate bash scripts with immutable locals, dispatch tables, pure functions, and zero mutable state.
Tasks:
- Clarify requirements — Purpose, I/O, shell type, args, error strategy, performance constraints.
- Read bash-scripting-guide.md — Strict mode, parameter expansion, arrays, bash 5.2/5.3 features.
- Read script-patterns.md — Argument parsing, logging, parallel processing, retry, signals.
- Read text-processing-guide.md — rg/awk/sd selection, pipeline patterns, performance.
- Structure script — Shebang + strict mode + readonly constants + trap + main.
- Implement — Core functions, business logic, main entry point.
- Validate —
bash -n script.sh, ShellCheck 0.11.0+, re-validate until clean.
[1][REQUIREMENTS]
Dictum: Ambiguity resolution prevents rework.
Clarify before generating:
| [INDEX] | [AMBIGUITY] | [QUESTION] |
|---|
| [1] | Data format | Input format? (nginx combined, JSON, CSV, custom) |
| [2] | Large files | Files >100MB? Optimize for memory/performance? |
| [3] | Error handling | Fail fast, continue with warnings, or retry? |
| [4] | Portability | POSIX sh portability or bash 5.2+/5.3? |
| [5] | Output format | Human-readable, JSON, or CSV? |
Guidance:
- Architecture First: Explain design, tool selection rationale, key tradeoffs before writing code.
- Data Format Routing: JSON?
jq. YAML? yq eval. CSV/TSV? miller (mlr). Interactive exploration? jnv.
- Template: Reference
assets/templates/standard-template.sh for production boilerplate.
[2][STRICT_MODE]
Dictum: Strict mode prevents silent failures.
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s inherit_errexit
IFS=$'\n\t'
Guidance:
- Scope: Every generated script includes this block. No exceptions.
- Disable: Temporarily suppress —
output=$(cmd 2>&1) || handle_error "${output}".
[3][FUNCTIONAL_STYLE]
Dictum: Immutability and dispatch tables eliminate mutable state.
| [INDEX] | [RULE] | [PATTERN] |
|---|
| [1] | Immutable locals | local -r for all non-mutating variables inside functions |
| [2] | Immutable globals | readonly for all module-level constants |
| [3] | Pure functions | Input via args, output via stdout or nameref (local -n), no global state |
| [4] | Dispatch tables | declare -Ar for O(1) routing; case/esac only for pattern matching |
| [5] | Higher-order | Pass function names as args; local -n nameref for array parameters |
| [6] | Inline trivials | Single-use < 3 lines: inline at call site |
| [7] | Brace grouping | { cmd1; cmd2; } > file over ( ... ) (no subshell) |
| [8] | No mutable counters | ${#arr[@]}, rg -c, or awk pipelines for counting |
| [9] | mapfile/readarray | Over while read loops for array population (3-5x faster) |
| [10] | printf everywhere | Over echo (handles escapes, format strings, no ambiguity) |
| [11] | $(<file) | Over $(cat file) (no fork) |
| [12] | printf -v | printf -v var '%(%F %T)T' -1 over $(date ...) (no subshell) |
| [13] | Here-strings | <<< over echo x | cmd pipelines |
| [14] | BASH_REMATCH | + over / |
Best-Practices:
- Nameref Constraint: Array variables cannot be namerefs, but namerefs can reference arrays.
- Dispatch Declaration:
declare -Ar assigns on declaration line; separate assignment fails for readonly.
- Structured Checks:
declare -Ar CHECKS=([name]="pattern\|msg\|level") + IFS=\| read -r for data-driven validation.
[4][QUALITY_GATE]
Dictum: Checklists prevent omissions.
[VERIFY] Generation:
[REFERENCE]: docs/bash-scripting-guide.md — Language features, parameter expansion, arrays.
[REFERENCE]: docs/script-patterns.md — Argument parsing, logging, parallel, retry.
[REFERENCE]: docs/text-processing-guide.md — Tool selection, rg/awk/sd, performance.