Tools: Complete Guide to Every bash script I write starts with the same 20 lines. So I made a generator for them.

Tools: Complete Guide to Every bash script I write starts with the same 20 lines. So I made a generator for them.

There's a set of things every bash script should have at the top. Not "here are some nice-to-haves" — actual things that prevent real, bad, hard-to-debug failures:

This is what I call the mandatory header. I've forgotten parts of it enough times that I stopped trying to remember it and started generating it.

The Boilerplate Generator

You configure:

Script identity — name, description, author. These go in the comment block at the top so someone reading the script 6 months from now (probably you) knows what it's supposed to do.

Safety flags — each one is a checkbox with an explanation of what it does. set -x is there too for debug mode — I often add it temporarily when something isn't working, then remove it before deploying.

Features:

Snippet injection — you can pull in a script from the library (disk space warning, file backup, delete old logs, etc.) and have it dropped into the body of the generated boilerplate. So you're not starting from zero — you're starting from a working script wrapped in production safety.

What the output looks like

I use this myself. Every new script starts here.

Why this exists as a tool instead of a GitHub gist

Because I kept updating my gist as I learned new things, and then had five different versions floating around in different places. A tool with checkboxes forces me to be explicit about what I'm including and why, and the output is always current.

Try it: https://bashsnippets.xyz/tools/bash-boilerplate-generator.html

Free, no login, GitHub Pages. Generates clean .sh output you can paste directly into a file and start editing. set -e makes the script exit immediately if any command returns a non-zero status. Without it, your script keeps running after a failure and you get compounding errors that are confusing to trace. set -u makes the script error if you reference a variable that hasn't been set. Without it, $MYVAR being undefined silently evaluates to an empty string. That empty string goes into a path. That path gets passed to rm -rf. You see where this goes. set -o pipefail makes the exit status of a pipeline reflect the first failure in the chain, not just the last command. broken_command | grep something would exit 0 without this flag, because grep succeeded even though the input was empty. IFS=$'\n\t' prevents word splitting on spaces in filenames. Without it, a filename like my file.txt gets treated as two arguments. Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to ? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well For further actions, you may consider blocking this person and/or reporting abuse

Code Block

Copy

#!/bin/bash set -euo pipefail IFS=$'\n\t' #!/bin/bash set -euo pipefail IFS=$'\n\t' #!/bin/bash set -euo pipefail IFS=$'\n\t' set -o pipefail broken_command | grep something IFS=$'\n\t' my file.txt - Timestamped log() function — stops you from using raw echo for output, which doesn't timestamp. When you're looking at a log file and everything just says "backup complete" with no time attached, you'll wish you had this. - CHECK / CROSS variables — adds CHECK="✓" and CROSS="✗" at the top, which is the convention across all BashSnippets scripts. Consistent visual output in the terminal. - Argument parser with --help and --dry-run — getopts-based. Dry run mode is something I use constantly during development to test a script's logic without actually writing/deleting/moving anything. - Lock file — prevents duplicate runs. If you're scheduling something with cron and the job sometimes runs long, without a lock file you can end up with two instances trying to write to the same output file. - Root check — exits cleanly with an error if the script isn't run as root, instead of failing halfway through with a permission error. - Trap on EXIT — runs a cleanup() function on exit or interruption, which you can populate with whatever teardown logic makes sense.