0
0
Bash Scriptingscripting~10 mins

Debugging with PS4 in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Debugging with PS4
Set PS4 variable
Enable debugging with 'set -x'
Run script commands
Each command prints with PS4 prefix
Observe output with debug info
Disable debugging with 'set +x'
Script continues normally
Set PS4 to customize debug output prefix, enable debugging with 'set -x', run commands to see debug info, then disable debugging.
Execution Sample
Bash Scripting
PS4='+ $BASH_SOURCE:$LINENO:${FUNCNAME[0]}: '
set -x
myfunc() {
  echo "Hello"
}
myfunc
set +x
This script sets PS4 to show file, line, and function name before each command, enables debugging, runs a function, then disables debugging.
Execution Table
StepCommand RunPS4 Prefix OutputDebug OutputNotes
1PS4='+ $BASH_SOURCE:$LINENO:${FUNCNAME[0]}: 'Set PS4 variable for debug prefix
2set -xEnable debugging mode
3myfunc() {+ script.sh:3:: myfunc() {Start function definition
4 echo "Hello"+ script.sh:4:: echo "Hello"During function definition: echo command
5}+ script.sh:5:: }End function definition
6myfunc+ script.sh:7:: myfuncCall function
7echo "Hello"+ script.sh:4:myfunc: HelloFunction prints Hello
8set +x+ script.sh:8:: set +xDisable debugging mode
9Debugging off, normal script continues
💡 Debugging stops after 'set +x', script runs normally without debug output.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 8Final
PS4default+ $BASH_SOURCE:$LINENO:${FUNCNAME[0]}: + $BASH_SOURCE:$LINENO:${FUNCNAME[0]}: + $BASH_SOURCE:$LINENO:${FUNCNAME[0]}: + $BASH_SOURCE:$LINENO:${FUNCNAME[0]}:
set -x (debug mode)offoffonoffoff
Key Moments - 3 Insights
Why does each debug line start with the PS4 prefix?
Because PS4 defines the prefix shown before each command when debugging is enabled with 'set -x', as seen in execution_table rows 3-7.
What happens if we don't disable debugging with 'set +x'?
Debug output continues for all commands, cluttering output. Execution_table row 9 shows debugging stops after 'set +x'.
Why does the function name appear in the PS4 prefix during the function call?
Because PS4 uses ${FUNCNAME[0]} which shows the current function name, visible in row 7 of execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the PS4 prefix output when the echo command inside the function runs?
A+ script.sh:4:myfunc:
B+ script.sh:7::
C+ script.sh:3::
D+ script.sh:8::
💡 Hint
Check execution_table row 7 under 'PS4 Prefix Output' column.
At which step does debugging get disabled?
AStep 2
BStep 6
CStep 8
DStep 9
💡 Hint
Look at execution_table row 8 where 'set +x' is run.
If PS4 was set to just '+ ', how would the debug output prefix change?
AIt would show file and line number before each command
BIt would show only '+ ' before each command
CIt would show function name only
DNo debug output would appear
💡 Hint
PS4 defines the prefix; changing it to '+ ' removes file, line, and function info.
Concept Snapshot
PS4 sets the prefix for debug output in bash.
Use 'set -x' to enable debugging and see commands with PS4 prefix.
Use 'set +x' to disable debugging.
PS4 can include variables like $BASH_SOURCE, $LINENO, ${FUNCNAME[0]}.
This helps trace script execution step-by-step.
Full Transcript
Debugging with PS4 in bash means setting the PS4 variable to customize the prefix shown before each command when debugging is enabled. You enable debugging with 'set -x', which prints each command with the PS4 prefix before running it. This helps you see exactly what the script is doing and where. You can include file name, line number, and function name in PS4 for detailed tracing. When done, disable debugging with 'set +x' to stop the extra output. This method helps find errors by showing the script's flow clearly.