The best way is to use set -xv
. The set -v
will echo a line before it is executed. The set -x
will output the line after the shell script interpolates the variables and expressions in that line.
As part of this, you can also create an environment variable called PS4
which will be the prompt printed each time your shell scripts outputs the line being executed. Most people set it to something like PS="\$LINENO: "
which will print out the line number for the line being executed.
Once you're finished, you can turn off debugging by setting set +xv
.
#
# Somewhere in this part of my script, I am having problems....
#
export PS4="\$LINENO> " # Now, I'll see the line numbers while debugging
set -xv # Debugging is turned on
....
#
# Done with debugging
#
set +xv # Debugging is turned off