set -x (long notation: set -o xtrace) traces commands before executing them.
The value of the PS4 variable is expanded and printed before the actual debug output.
In the following example, PS4 uses $(date …) to include the actual value of the hour, minute and second. It also uses ANSI escape sequences to distinguish between script output and debug output.
run.sh
echo \$-=$-
# set -x
set -o xtrace
echo \$-=$-
#
# The PS4 variable specifies what's printed before
#
PS4=' \e[33m$(date +"%H:%M:%S"): $BASH_SOURCE@$LINENO ${FUNCNAME[0]} -> \e[0m'
run_numbered_script() {
local script_no=$1
./script-$script_no.sh
}
run_numbered_script 1
for var in $(seq 5); do
sleep 1
if [ $var -eq 4 ]; then
echo "var is now four"
fi
done
run_numbered_script 1