📝 Bash Script Generator

🎯 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

No arguments. Click "Add argument" to create one.

🧱 Building blocks

📝 Script body

📤 Generated script

🐚 Lint with ShellCheck
{{ 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 after tar failed.
  • 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 so foo | bar fails if foo fails, not just if bar does.
  • IFS=$'\n\t' — restrict word-splitting to newlines and tabs so spaces in filenames stop breaking for loops.

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 on PATH before 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 on EXIT, INT, and TERM so partial state is removed even on Ctrl-C.
  • Usage function — generates a usage() function and wires -h / --help to 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 flock on 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, (( ... )), and source are bash extensions. The generator detects them in your body when the shebang is #!/bin/sh and warns accordingly.
  • Trap without tmpdir — the trap-cleanup block expects TMPDIR to 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.sh does the trick.
  • POSIX getopts can't do long options — this tool emits a manual case "$1" in loop instead, which works in both bash and sh.

Source references

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"
                ]
            }
        }
    ]
}
Ready