< Previous | Contents | Next >
Examining Values During Execution
It is often useful, along with tracing, to display the content of variables to see the internal workings of a script while it is being executed. Applying additional echo statements will usually do the trick:
#!/bin/bash
# trouble: script to demonstrate common errors number=1
echo "number=$number" # DEBUG
set -x # Turn on tracing if [ $number = 1 ]; then
echo "Number is equal to 1." else
echo "Number is not equal to 1."
fi
set +x # Turn off tracing
#!/bin/bash
# trouble: script to demonstrate common errors number=1
echo "number=$number" # DEBUG
set -x # Turn on tracing if [ $number = 1 ]; then
echo "Number is equal to 1." else
echo "Number is not equal to 1."
fi
set +x # Turn off tracing
In this trivial example, we simply display the value of the variable number and mark the added line with a comment to facilitate its later identification and removal. This tech- nique is particularly useful when watching the behavior of loops and arithmetic within scripts.