How to use the Bash Script Boilerplate Generator
- 1. Name the scriptGive it a file name and a one-line description.
- 2. Define flagsList letter:name pairs such as s:source; verbose and force become on/off switches.
- 3. Fill in main()Download, chmod +x, and write your commands where the TODO is.
What the default script contains
The default backup.sh is summarised as 3 flags in strict mode: -s for source and -d for dest, which take values, and -v for verbose, which is a switch. The script opens with set -Eeuo pipefail, so it stops on any failing command, any unset variable and any failure inside a pipe. A usage function prints help for -h, and the getopts loop reports missing values and unknown flags with a clear message.
It then checks that tar and date exist, creates a temporary folder with mktemp and removes it with a trap on exit, even if the script fails halfway. Inside main, -s and -d are checked as required before your commands run.
Why these pieces matter
- Without -e, bash carries on after an error, and a failed cd followed by rm can delete the wrong files.
- Without -u, a typo in a variable name silently becomes an empty string.
- pipefail makes
cmd | tee logfail when cmd fails, not just when tee does. - Logging to stderr keeps messages out of output you might pipe elsewhere.
We checked the generated default with bash -n for syntax errors. Run shellcheck on your finished script to catch quoting mistakes. Build individual commands with the Linux command builder, and run the script on a schedule with the cron expression generator or a systemd service.
Frequently asked questions
Does this work with sh or zsh?
It is written for bash. The shebang uses /usr/bin/env bash, so run it as ./script.sh or bash script.sh, not sh script.sh.
Why getopts and not long options like --source?
getopts is built into bash and works everywhere. Long options need extra parsing code or GNU getopt.
Is the Bash Script Boilerplate Generator free?
Yes, the Bash Script Boilerplate Generator is free. The first time you generate your first script, you are asked for your name and WhatsApp number once. After that, this and every other Uncodemy tool opens straight away without the form.