Bash Snippet Builder

Build a safe Bash script from tested pieces: options, logging, traps, locks, retries
Tick the pieces you want and get one complete Bash script with them put together the right way: strict mode, --long options and help, logging, a cleanup trap, a lock so only one copy runs, retries, a dry-run switch, a yes/no prompt, a safe file loop, a config file reader and parallel jobs. Every piece is explained, and the scripts it builds pass ShellCheck.
RUNS ENTIRELY IN YOUR BROWSER · NOTHING IS SENT ANYWHERE

Script

Options

Your own options

Leave “Value” empty for an on/off switch. --output-dir is stored in $OUTPUT_DIR.

Pieces

If already running, wait seconds (0 = stop at once)
Files named
at a time (--jobs)

Your script


    

Then: chmod +x myscript.sh && ./myscript.sh --help

What each part does

Bash mistakes worth knowing

A safe Bash script skeleton in one go

Tick the pieces a script needs, such as option parsing, a lock so only one copy runs, retries or a dry-run switch, and get one complete script with them wired together in the right order. Each part is explained below the script, and the scripts it builds pass ShellCheck.

Common questions

What does set -euo pipefail do?

-e stops the script when a command fails, -u stops it when it uses a variable that was never set, and -o pipefail makes a pipeline fail when any command in it fails, not just the last. It’s a safety net with gaps: failures inside if tests and &&/|| lists don’t stop the script.

How do I parse long options like --verbose in Bash?

The built-in getopts only handles short options like -v. For --long options, loop over the arguments with while (( $# )); do case $1 in … esac; shift; done, which also lets you accept --name=value. The external getopt command can do it too, but the one on macOS can’t.

How do I make sure only one copy of a script runs at a time?

Use flock: exec 9>>/run/lock/myscript.lock then flock -n 9 || exit 1. The kernel releases the lock as soon as the script ends, even after a crash, so there’s no stale PID file to clean up. In a crontab, flock -n /tmp/job.lock /path/to/script does the same.

How do I clean up temporary files when a Bash script exits?

Make a private folder with tmp=$(mktemp -d) and add trap 'rm -rf -- "$tmp"' EXIT. The EXIT trap runs when the script finishes, fails, or is stopped with Ctrl+C or kill. Nothing runs after kill -9.

How do I retry a failing command in Bash?

Wrap it in a function with an until loop that sleeps and doubles the wait after each failure, and gives up after a set number of tries. Count with n=$((n + 1)) rather than ((n++)): when n is 0, ((n++)) returns 1 and set -e ends the script.

Related tools: Cron Orchestrator · PowerShell ↔ Bash Translator · All tools A–Z