0
0
Bash Scriptingscripting~5 mins

Debugging with PS4 in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the PS4 variable in bash scripting?
PS4 is a special variable in bash that defines the prompt used when debugging with 'set -x'. It helps show detailed trace information for each command executed.
Click to reveal answer
beginner
How do you enable debugging mode in a bash script to see command traces?
You enable debugging by running 'set -x' in the script or shell. This makes bash print each command and its arguments before executing them.
Click to reveal answer
intermediate
How can you customize the debugging output prefix using PS4?
You can set PS4 to any string you want. For example, 'PS4="+ $(date +%T) "' adds a timestamp before each debug line, making it easier to follow.
Click to reveal answer
intermediate
What does the following PS4 value do? PS4='+ ${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
This PS4 setting shows the script file name, line number, and function name before each debug line. It helps locate where commands run in the script.
Click to reveal answer
beginner
How do you turn off debugging mode after using 'set -x'?
You turn off debugging by running 'set +x'. This stops bash from printing commands before execution.
Click to reveal answer
What does the PS4 variable control in bash debugging?
AThe error message format
BThe shell prompt when not debugging
CThe output color of the script
DThe prompt shown before each debug command trace
Which command enables debugging mode in bash?
Adebug on
Bset +x
Cset -x
Dtrace enable
How can you add a timestamp to each debug line in bash?
ASet PS4 to include $(date +%T)
BUse set +x with a timestamp
CChange the shell prompt PS1
DUse echo before each command
What does 'set +x' do in a bash script?
AStarts debugging mode
BStops debugging mode
CPrints all variables
DRuns the script silently
Which PS4 setting helps show the script file and line number in debug output?
APS4='+ ${BASH_SOURCE}:${LINENO} '
BPS4='DEBUG>'
CPS4='Line:'
DPS4='${FUNCNAME[0]}'
Explain how to use PS4 and set -x together to debug a bash script with detailed trace information.
Think about how PS4 changes the prefix and set -x turns on tracing.
You got /4 concepts.
    Describe how you would stop debugging output after enabling it with set -x in a bash script.
    Consider the opposite of set -x.
    You got /3 concepts.