Debugging bash scripts

If you want to see the output of a bash script as it's being executed you can use set -x to enable debugging and set +x to turn it off again. This is particularly handy when you're trying to debug why variables aren't being interpolated correctly.

For example if you had a script like below:

#/bin/bash

PASSWORD="Sup3r $ecret Passw0rd!"

echo $PASSWORD

The output will be:

Sup3r Passw0rd!

The $ hasn't been escaped. If you enable debugging with:

#/bin/bash

PASSWORD="Sup3r $ecret Passw0rd!"

set -x

echo $PASSWORD

The output is now:

++ echo Sup3r 'Passw0rd!'
Sup3r Passw0rd!

It doesn't identify the issue by itself, however it shows the commands as they're being executed.