Paper Almanac

Checking shell scripts before they run

October 2026

A shell script fails in an unpleasant way far more often when it is first run on the machine it was written for. Three checks, in order of how little they cost, catch most of those failures.

Parse only

The interpreter can read the file without doing anything in it:

bash -n script.sh

This finds a missing fi, an unterminated quote or a stray parenthesis. It runs in a blink, so there is no reason to skip it.

Ask a linter

ShellCheck reads the script the way an experienced reviewer would and points at unquoted variables, uses of commands that may not exist and logic that will not do what the indentation suggests. Its explanations are worth reading once; after a while the same remarks stop appearing.

Strict mode

Near the top of the file:

set -euo pipefail

A failed command now stops the script instead of letting the next one run on top of the mess. It does change what a pipe means, so a script written without it may need a few lines adjusted.

Then a dry run

Run the script against a scratch directory, with every real path replaced by a temporary one. Scripts that write files should earn trust the same way anything else does: once, on something that does not matter.

All notes