scripts

Checklist for a script that will be run by someone other than you

1. Declare the interpreter

#!/usr/bin/env bash

env finds Bash on the PATH Hardcoded interpreter paths break on systems that put things elsewhere

2. Turn on the guardrails

set -euo pipefail

-e stops on an error -u stops on an unset variable -o pipefail stops when any part of a pipeline fails

3. Read arguments with a default

label="${1:-default}"
printf '%s\n' "$label"

No argument gives default An explicit argument is used as-is

4. Test the whole file

bash -n script.sh

-n checks syntax without running anything Run this before handing a script to anyone

5. Actually run it

The script in this series took one argument and printed it back Exit status was zero That is the entire bar for a small utility

Next

Small recipes in 10 toolkit

Posted