Skip to content

Debugging shell scripts: bash -x, set -euo pipefail and trap

NinjaLesson 52 of 6610 min

A backup script says it worked and didn’t. Trace it with bash -x, make it stop at the first error with set -euo pipefail, learn trap, and fix it.

The worst kind of script is the one that fails and then tells you it worked. ~/bin/backup.sh is one of those. Run it:

~/bin/backup.sh

An error scrolls past, then a cheerful "Backed up 3 notes". Is anything in ~/backup? Reading the script line by line is one way to find out why. Watching it run is faster.

Watch it run: bash -x

bash -x script prints every command just before it runs, after the variables have been filled in, each line starting with + (++ for a command inside $( )). You see what the script really did, not what you think it says:

bash -x ~/bin/backup.sh

Look at the cp line in the trace and compare it with the script. To trace only part of a script, put set -x before the part and set +x after it.

Make it stop at the first error

By default, bash carries on after a command fails. Three settings change that, and most careful scripts start with all three, right under the #! line:

set -euo pipefail
SettingWhat it does
-estop the script as soon as a command fails
-utreat a variable that was never set as an error, instead of quietly using nothing
-o pipefaila pipeline fails if any part of it fails, not only the last

-u alone would have caught this bug before it did anything.

Say where it failed: trap

A trap runs a command when something happens. ERR fires when a command fails and EXIT when the script ends, however it ends, which is the place to clean up temporary files:

trap 'echo "backup.sh failed on line $LINENO" >&2' ERR
trap 'rm -f /tmp/backup.lock' EXIT

Try one at the prompt:

bash -c 'trap "echo something failed" ERR; false; echo the script carried on'

Tip:You can edit the script with vim, or with sed -i. sed -i '2i set -euo pipefail' file inserts a line before line 2.

Your turn

Trace ~/bin/backup.sh with bash -x to see what it really runs. Add set -euo pipefail near the top so it stops at the first error, fix the bug, and run it so the three notes land in ~/backup.

Next