Credit: I learned all of these tips from Julia Evans, via this comic she wrote.

The following instructs bash to print out every line of a script as it executes, with all variables expanded.

#!/bin/bash
set -x

You can also run the script with the -x option for the same effect.

bash -x script.sh

To make the script run one line at a time, use the DEBUG signal, which is triggered before every line of code:

trap read DEBUG

Even fancier, put this at the start of your script to confirm every line before it runs:

trap '(read -p "[$BASH_SOURCE:$LINENO] $BASH_COMMAND")' DEBUG

Error messages

This is a handy function for erroring out of a script:

die() { echo $1 >&2; exit 1; }
 
# example usage
# the program will exit and "oh no!" will print if `some_command` fails
some_command || die "oh no!"