Challenge - 5 Problems
Trace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output when using set -x in a simple script?
Consider this bash script snippet:
What will be printed to the terminal when this script runs?
#!/bin/bash set -x x=5 y=3 z=$((x + y)) echo $z
What will be printed to the terminal when this script runs?
Bash Scripting
#!/bin/bash set -x x=5 y=3 z=$((x + y)) echo $z
Attempts:
2 left
💡 Hint
Trace mode prints each command with a plus sign before executing it.
✗ Incorrect
The set -x command enables trace mode, which prints each command preceded by a plus sign (+) before it runs. So you see the assignments and the echo command with +, then the output 8.
💻 Command Output
intermediate2:00remaining
What happens if set -x is turned off mid-script?
Given this bash script:
What will be the output including trace lines?
#!/bin/bash set -x var=10 set +x var2=20 echo $var $var2
What will be the output including trace lines?
Bash Scripting
#!/bin/bash set -x var=10 set +x var2=20 echo $var $var2
Attempts:
2 left
💡 Hint
set +x disables trace mode, so commands after it won't show trace.
✗ Incorrect
Trace mode is on until set +x disables it. So only the first assignment shows with +, the second does not. The echo command still runs and prints 10 20.
🔧 Debug
advanced2:00remaining
Why does this script not show trace output after set -x?
Look at this script:
Why is there no trace output for the echo inside the if block?
#!/bin/bash set -x if false; then echo "Won't run" fi set +x echo "Done"
Why is there no trace output for the echo inside the if block?
Bash Scripting
#!/bin/bash set -x if false; then echo "Won't run" fi set +x echo "Done"
Attempts:
2 left
💡 Hint
Trace shows commands that actually run.
✗ Incorrect
The echo inside the if block is skipped since the condition is false. So no trace output appears for it.
💻 Command Output
advanced2:00remaining
What is the output of this script with nested set -x and set +x?
Analyze this script:
What is the full output including trace lines?
#!/bin/bash
set -x
var=1
{
set +x
var=2
echo $var
}
echo $varWhat is the full output including trace lines?
Bash Scripting
#!/bin/bash set -x var=1 { set +x var=2 echo $var } echo $var
Attempts:
2 left
💡 Hint
set +x disables trace inside the block, but echo outside still traces.
✗ Incorrect
Trace is on initially, so var=1 shows with +. Inside the block, set +x disables trace, so var=2 and echo $var run silently printing 2. After block, echo $var runs with trace showing + echo 2 and prints 2.
🧠 Conceptual
expert2:00remaining
How does set -x help in debugging complex scripts?
Which of these best explains the main benefit of using set -x in bash scripts?
Attempts:
2 left
💡 Hint
Think about what trace mode does when enabled.
✗ Incorrect
set -x prints each command with its arguments before running it, so you can see exactly what the script does step-by-step. This helps find where things go wrong.