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
✗ Incorrect
PS4 sets the prefix shown before each command line when debugging with 'set -x'.
Which command enables debugging mode in bash?
Adebug on
Bset +x
Cset -x
Dtrace enable
✗ Incorrect
'set -x' turns on debugging, showing commands as they run.
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
✗ Incorrect
Setting PS4 to '+ $(date +%T) ' adds a timestamp prefix to debug output.
What does 'set +x' do in a bash script?
AStarts debugging mode
BStops debugging mode
CPrints all variables
DRuns the script silently
✗ Incorrect
'set +x' disables debugging mode and stops printing commands.
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]}'
✗ Incorrect
Using '${BASH_SOURCE}:${LINENO}' in PS4 shows the file and line number for each command.
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.
Practice
(1/5)
1. What does the PS4 variable control in a bash script when debugging?
easy
A. The number of commands to debug before stopping
B. The output file where debug logs are saved
C. The timeout duration for debugging
D. The prefix shown before each command when debugging is enabled
Solution
Step 1: Understand the role of PS4 in bash debugging
PS4 sets the prefix string that appears before each command when debugging with set -x is active.
Step 2: Differentiate PS4 from other debugging settings
PS4 does not control output files, timeouts, or command counts; it only changes the debug output prefix.
Final Answer:
The prefix shown before each command when debugging is enabled -> Option D
Quick Check:
PS4 = debug output prefix [OK]
Hint: PS4 sets what you see before each debug command line [OK]
Common Mistakes:
Confusing PS4 with output file settings
Thinking PS4 controls debug duration
Assuming PS4 limits number of debug commands
2. Which of the following is the correct way to set a custom PS4 prefix to show the current line number in bash debugging?
easy
A. PS4='Line \$LINENO: '
B. PS4='Line $LINENO: '
C. PS4='Line \n: '
D. PS4='Line $LINENO\n'
Solution
Step 1: Understand variable expansion in PS4
To show the current line number dynamically, you must escape the dollar sign in PS4 so it expands at runtime, not when assigned.
Step 2: Identify correct escaping
Using PS4='Line \$LINENO: ' escapes the dollar sign, so $LINENO expands during debugging, showing the actual line number.
Final Answer:
PS4='Line \$LINENO: ' -> Option A
Quick Check:
Escape $ for runtime expansion in PS4 [OK]
Hint: Escape $ in PS4 to expand variables during debugging [OK]
Common Mistakes:
Not escaping $ causes literal $LINENO output
Using \n instead of $LINENO for line number
Placing newline characters incorrectly
3. Given this script snippet with debugging enabled:
PS4='+ $BASH_SOURCE:$LINENO:'
set -x
x=5
y=10
z=$((x + y))
set +x
What is the first debug output line shown after set -x?
medium
A. + script.sh:2: x=5
B. + : x=5
C. + :2: x=5
D. + script.sh:x=5
Solution
Step 1: Analyze PS4 value and variable expansion
PS4 is set to '+ $BASH_SOURCE:$LINENO:' but $BASH_SOURCE and $LINENO are not escaped, so they expand immediately when assigned, likely empty or current shell values.
Step 2: Determine actual debug prefix
Because variables are not escaped, $BASH_SOURCE is empty and $LINENO is 2 (line of set -x), so prefix becomes '+ :2:'.
Final Answer:
+ :2: x=5 -> Option C
Quick Check:
Unescaped vars expand early, prefix = '+ :2:' [OK]
Hint: Unescaped vars in PS4 expand immediately, not during debug [OK]
Common Mistakes:
Assuming $BASH_SOURCE expands to script filename
Expecting line number to be dynamic without escaping
Ignoring immediate expansion on assignment
4. You wrote this script to debug with line numbers:
PS4='Line $LINENO: '
set -x
foo=bar
set +x
But the debug output shows Line $LINENO: foo=bar literally. What is the fix?
medium
A. Change PS4 to 'Line \$LINENO: ' to delay variable expansion
B. Remove quotes around PS4 assignment
C. Use single quotes instead of double quotes for PS4
D. Set PS4 after set -x
Solution
Step 1: Identify why $LINENO is literal
Using single quotes with unescaped $LINENO causes it to be treated as literal text, not expanded during debugging.
Step 2: Fix by escaping $ for runtime expansion
Escaping $ as \$ in PS4 delays expansion until debugging runs, showing actual line numbers.
Final Answer:
Change PS4 to 'Line \$LINENO: ' to delay variable expansion -> Option A
Quick Check:
Escape $ in PS4 to expand at debug time [OK]
Hint: Escape $ in PS4 to see line numbers during debugging [OK]
Common Mistakes:
Not escaping $ in PS4
Changing quotes incorrectly
Setting PS4 after enabling debugging
5. You want to debug a bash script showing the filename, line number, and function name before each command. Which PS4 setting achieves this correctly?
hard
A. PS4='+ ${BASH_SOURCE}:${LINENO}:${FUNCNAME[0]}: '
B. PS4='+ \${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: '
C. PS4='+ $BASH_SOURCE:$LINENO:$FUNCNAME: '
D. PS4='+ \$BASH_SOURCE:\$LINENO:\$FUNCNAME: '
Solution
Step 1: Understand variable expansion timing in PS4
To show dynamic info like filename, line number, and function name during debugging, variables must be escaped with backslashes to delay expansion until debug time.
Step 2: Choose correct syntax for array variable FUNCNAME
Use \${FUNCNAME[0]} to get current function name properly escaped.
Step 3: Verify full PS4 string
PS4='+ \${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: ' correctly escapes all variables and uses braces for array indexing, ensuring dynamic debug info appears.
Final Answer:
PS4='+ \${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: ' -> Option B
Quick Check:
Escape all vars with braces for dynamic debug info [OK]
Hint: Escape variables with braces in PS4 for dynamic debug info [OK]
Common Mistakes:
Not escaping variables causing static output
Missing braces for array FUNCNAME
Using $FUNCNAME without index for function name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.