DEV Community

Anguishe
Anguishe

Posted on • Originally published at bashsnippets.xyz

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:

#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
Enter fullscreen mode Exit fullscreen mode

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.

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:

  • 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.

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

When you configure everything and click copy, you get a complete .sh file: shebang, comment block, safety flags, feature code, and your injected snippet (if any) in the main logic section. There's even a source comment pointing back to bashsnippets.xyz so your team knows where the template came from.

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.

Top comments (0)