Challenge - 5 Problems
PS4 Debugging Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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
Attempts:
2 left
💡 Hint
Remember PS4 shows the line number of the command being executed.
✗ Incorrect
PS4 is set to '+$LINENO: ' so each debug line shows the line number. The first command executed after setting PS4 is the function call at line 7.
📝 Syntax
intermediate1: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?
Attempts:
2 left
💡 Hint
Check how to properly quote the format string inside date command.
✗ Incorrect
Option A correctly quotes the format string with double quotes inside single quotes, so date +"%T" runs properly. Option A lacks quotes around %T causing error.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Think about when PS4 is expanded and how set -x uses it.
✗ Incorrect
PS4 is expanded once when set -x is enabled. Changing PS4 inside the loop does not affect the debug prompt because the shell uses the original PS4 value.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
FUNCNAME array holds the call stack, index 0 is current function.
✗ Incorrect
FUNCNAME[1] is the caller function name, so to show the function where the command runs, use FUNCNAME[1]. FUNCNAME[0] is the current function (the debug function itself).
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about when command substitution runs in variable assignments.
✗ Incorrect
Command substitution in PS4 is evaluated once when PS4 is assigned, so the timestamp is fixed for the entire debug session.