Debugging shell scripts: bash -x, set -euo pipefail and trap
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.shAn 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.shLook 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
| Setting | What it does |
|---|---|
-e | stop the script as soon as a command fails |
-u | treat a variable that was never set as an error, instead of quietly using nothing |
-o pipefail | a 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.
Your task
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.
- Trace the script and see its cp line (not done yet)
- The script has set -euo pipefail (not done yet)
- Fixed and run: the notes are in ~/backup (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
bash -x- Print each command, after expansion, before it runs (debugging)
set -x- Print each command, after expansion, with a + in front
set -e- Exit when a command fails
set -u- An unset variable is an error
trap- Run commands when a script exits or a command fails
Every everyday command on one page: the Linux commands cheat sheet.