Search notes:

Bash built-in: set -x (set -o xtrace)

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
Github repository about-Bash, path: /built-in/set/x/run.sh

script-1.sh

echo this is script-1, \$-=$-

for var in $(seq 2); do
  if [ $var -eq 1 ]; then
    echo "script 1, var=$var"
  fi
done
Github repository about-Bash, path: /built-in/set/x/script-1.sh

script-2.sh

echo this is script-2, \$-=$-
for var in $(seq 2); do
  if [ $var -eq 1 ]; then
    echo "script 2 calls script-3"
    ./script-3.sh
  fi
done

Github repository about-Bash, path: /built-in/set/x/script-2.sh

script-3.sh

echo this is script-3, \$-=$-
Github repository about-Bash, path: /built-in/set/x/script-3.sh

See also

Bash builtins

Index