Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Debugging with PS4 in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PS4 Debugging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this script with PS4 set?
Consider this bash script with PS4 set to show line numbers. What will be the first line printed when running with bash -x script.sh?
Bash Scripting
#!/bin/bash
PS4='+$LINENO: '

function greet() {
  echo "Hello, $1"
}
greet World
A
+2: function greet() {
+3: greet World
Hello, World
B
+3: greet World
+4: echo Hello, World
Hello, World
C
+4: greet World
+5: echo Hello, World
Hello, World
D
+1: PS4='+$LINENO: '
+2: function greet() {
+3: echo Hello, World
Hello, World
Attempts:
2 left
💡 Hint
Remember PS4 shows the line number of the command being executed.
📝 Syntax
intermediate
1:30remaining
Which PS4 assignment is valid to show timestamp before each command?
You want to set PS4 to show the current time in HH:MM:SS format before each command in bash debugging. Which assignment is valid?
APS4='$(date +"%T") '
BPS4='$(date +%T) '
CPS4='$(date +%T)\n'
DPS4='$(date +%T)\t'
Attempts:
2 left
💡 Hint
Check how to properly quote the format string inside date command.
🔧 Debug
advanced
2:30remaining
Why does this PS4 setting cause no output in bash -x?
Given this script: #!/bin/bash PS4='+$LINENO: ' set -x for i in {1..3}; do echo $i PS4='+$LINENO: ' done Why does the debug output not show line numbers inside the loop?
Bash Scripting
#!/bin/bash
PS4='+$LINENO: '
set -x

for i in {1..3}; do
  echo $i
  PS4='+$LINENO: '
done
ABecause PS4 is expanded once when set, so changing it inside the loop has no effect
BBecause set -x disables PS4 inside loops
CBecause PS4 is reset inside the loop after set -x, so it doesn't apply to commands inside the loop
DBecause the shell ignores PS4 when used inside loops
Attempts:
2 left
💡 Hint
Think about when PS4 is expanded and how set -x uses it.
🚀 Application
advanced
2:00remaining
How to modify PS4 to show function names in bash debug output?
You want your bash debug output to include the function name before each command. Which PS4 setting achieves this?
APS4='+${FUNCNAME[0]}: '
BPS4='+${FUNCNAME}: '
CPS4='+${FUNCNAME[-1]}: '
DPS4='+${FUNCNAME[1]}: '
Attempts:
2 left
💡 Hint
FUNCNAME array holds the call stack, index 0 is current function.
🧠 Conceptual
expert
3:00remaining
What is the effect of this PS4 setting with command substitution?
Consider this PS4 setting: PS4='+$(date +"%T"): ' What happens when you run bash -x script.sh with this PS4?
AThe timestamp is ignored and PS4 shows only '+'
BThe timestamp is evaluated before each command, showing current time dynamically
CThe timestamp is evaluated once when PS4 is set, so all debug lines show the same time
DThe script fails with syntax error due to command substitution in PS4
Attempts:
2 left
💡 Hint
Think about when command substitution runs in variable assignments.

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

  1. 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.
  2. 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.
  3. Final Answer:

    The prefix shown before each command when debugging is enabled -> Option D
  4. 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

  1. 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.
  2. Step 2: Identify correct escaping

    Using PS4='Line \$LINENO: ' escapes the dollar sign, so $LINENO expands during debugging, showing the actual line number.
  3. Final Answer:

    PS4='Line \$LINENO: ' -> Option A
  4. 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

  1. 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.
  2. 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:'.
  3. Final Answer:

    + :2: x=5 -> Option C
  4. 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

  1. 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.
  2. Step 2: Fix by escaping $ for runtime expansion

    Escaping $ as \$ in PS4 delays expansion until debugging runs, showing actual line numbers.
  3. Final Answer:

    Change PS4 to 'Line \$LINENO: ' to delay variable expansion -> Option A
  4. 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

  1. 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.
  2. Step 2: Choose correct syntax for array variable FUNCNAME

    Use \${FUNCNAME[0]} to get current function name properly escaped.
  3. 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.
  4. Final Answer:

    PS4='+ \${BASH_SOURCE}:\${LINENO}:\${FUNCNAME[0]}: ' -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard