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 -xYou can also run the script with the -x option for the same effect.
bash -x script.shTo make the script run one line at a time, use the DEBUG signal, which is triggered before every line of code:
trap read DEBUGEven fancier, put this at the start of your script to confirm every line before it runs:
trap '(read -p "[$BASH_SOURCE:$LINENO] $BASH_COMMAND")' DEBUGError 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!"