Debugging Scripts
PS4 Prefix
Shape Trace Lines
PS4 controls the prefix Bash uses for execution traces. Modeling the prefix helps explain set -x output without printing noisy traces.
Program
Play the script to choose a trace prefix and build one modeled trace line.
ps4_prefix.sh
Replay: real traced execution (multi-file project)
#!/usr/bin/env bash
prefix="+ "
command="echo ready"
trace_line="${prefix}${command}"
echo "$trace_line"
#!/usr/bin/env bash
prefix="TRACE> "
command="echo ready"
trace_line="${prefix}${command}"
echo "$trace_line"
prefix ← +
3prefix="+ "4command="echo ready"values this step+ prefixcommand ← echo ready
3prefix="+ "4command="echo ready"5trace_line="${prefix}${command}"values this stepecho readycommandtrace_line ← + echo ready
4command="echo ready"5trace_line="${prefix}${command}"6echo "$trace_line"values this step+ echo readytrace_line+ prefixecho readycommandecho "$trace_line"
5trace_line="${prefix}${command}"6echo "$trace_line"output+ echo readyvalues this step+ echo readytrace_line
prefix ← TRACE>
3prefix="TRACE> "4command="echo ready"values this stepTRACE> prefixcommand ← echo ready
3prefix="TRACE> "4command="echo ready"5trace_line="${prefix}${command}"values this stepecho readycommandtrace_line ← TRACE> echo ready
4command="echo ready"5trace_line="${prefix}${command}"6echo "$trace_line"values this stepTRACE> echo readytrace_lineTRACE> prefixecho readycommandecho "$trace_line"
5trace_line="${prefix}${command}"6echo "$trace_line"outputTRACE> echo readyvalues this stepTRACE> echo readytrace_line
PS4
`PS4` is the prompt prefix Bash prints before traced commands.
set -x
`set -x` emits commands as Bash expands and runs them.
modeled trace
This example prints one trace-shaped line instead of enabling global tracing.