🎯 Preset
Select a curated starting point. The form fields below repopulate when you switch presets.
🔧 Shebang
🏷️ Script header
🛡️ Strict mode (set -euo pipefail)
Emits set -euo pipefail and IFS=$'\n\t'. Strongly recommended.
⌨️ Command-line arguments
🧱 Building blocks
📝 Script body
📤 Generated script
{{ output || '# Toggle building blocks on the left to generate a script.' }}
After saving: chmod +x {{ scriptName || 'script.sh' }} to make it executable.
⚠️ Warnings
- {{ w }}
📖 Explanation
-
{{ e.section }}
{{ e.meaning }}
ℹ️ About Bash Script Generator
What this generator does
This tool assembles a runnable shell script from a structured form: pick a shebang
(#!/bin/bash, #!/bin/sh, #!/usr/bin/env bash, or
#!/bin/zsh), toggle strict mode (set -euo pipefail), declare
command-line arguments, and enable common building blocks like a root check, dependency
check, log setup, mktemp -d + trap cleanup, usage function,
retry loop, parallel runner, flock lock file, and an internet connectivity
check. The output is a complete script you can save, chmod +x, and run.
Every emitted block follows the Google Shell Style Guide and is designed to pass ShellCheck without warnings. The "Lint with ShellCheck" button below opens the linter site so you can paste the generated script for a second opinion.
Why bash strict mode (set -euo pipefail)?
set -e— exit immediately when any command exits non-zero. Stops a backup script from continuing aftertarfailed.set -u— treat references to unset variables as an error. Catches typos in${ARCHIVE_PATH}vs.${ARCHIV_PATH}at run time.set -o pipefail— propagate failures through pipelines sofoo | barfails iffoofails, not just ifbardoes.IFS=$'\n\t'— restrict word-splitting to newlines and tabs so spaces in filenames stop breakingforloops.
See "Use Bash Strict Mode" (Aaron Maxwell)
for the canonical explanation. POSIX sh supports -e and -u
but not -o pipefail; this tool downgrades automatically when the shebang is
#!/bin/sh and warns about bash-only features detected in your body.
Building blocks
- Check root — refuses to run unless
$EUID -eq 0; useful for system maintenance scripts. - Check dependencies — verifies that required commands (
curl,jq, etc.) are onPATHbefore running anything. - Log setup — defines a
log()helper that timestamps each line and tees to a log file. - Tmpdir — creates a private working directory with
mktemp -d; pairs with trap-cleanup to auto-remove on exit. - Trap cleanup — registers a
cleanup()function onEXIT,INT, andTERMso partial state is removed even on Ctrl-C. - Usage function — generates a
usage()function and wires-h/--helpto it. - Retry loop — exponential-backoff retry helper that re-runs a command up to N times.
- Parallel runner — backgrounds tasks with a bounded concurrency limit and waits for all of them.
- Lock file — uses
flockon a lock file so two copies of the script cannot run at once (e.g. a cron job that overlaps with itself). - Check internet — pings a well-known host once before doing network-dependent work, fails fast on offline runs.
Common pitfalls
- sh vs bash mismatches —
[[ ... ]], arrays,(( ... )), andsourceare bash extensions. The generator detects them in your body when the shebang is#!/bin/shand warns accordingly. - Trap without tmpdir — the trap-cleanup block expects
TMPDIRto exist; enable the tmpdir block too or the trap will be a no-op. - Disabling strict mode — fine for porting legacy scripts, but you lose the safety net. The output includes warnings reminding you what you gave up.
- Forgetting
chmod +x— the file needs the executable bit set before you can run it directly.chmod +x my-script.shdoes the trick. - POSIX
getoptscan't do long options — this tool emits a manualcase "$1" inloop instead, which works in both bash and sh.
Source references
- Bash Reference Manual — canonical bash syntax reference (GNU).
- POSIX sh(1) specification — what is portable across all POSIX shells.
- Google Shell Style Guide — conventions this generator follows.
- ShellCheck — static analysis for shell scripts; paste the output to lint it.
- "Use Bash Strict Mode" (Aaron Maxwell) —
why
set -euo pipefailis the right default. - "Writing Robust Shell Scripts" (David Pashley) — traps, locking, idempotency.
API Usage Example:
POST /api/tools/linux/bash-script-generator
Content-Type: application/json
{
"shebang": "#!/bin/bash",
"strictMode": true,
"name": "backup.sh",
"args": [
{
"name": "source",
"short": "s",
"long": "source",
"type": "file",
"required": true
}
],
"blocks": [
{
"type": "usage-function"
},
{
"type": "check-deps",
"config": {
"deps": [
"tar",
"gzip"
]
}
}
]
}