Bird
Raised Fist0
Bash Scriptingscripting~20 mins

set -x for trace mode in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when using set -x in a simple script?
Consider this bash script snippet:
#!/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
A
+ x=5
+ y=3
+ z=8
8
B8
C
+ x=5
+ y=3
+ z=8
+ echo 8
8
D
x=5
y=3
z=8
echo 8
8
Attempts:
2 left
💡 Hint
Trace mode prints each command with a plus sign before executing it.
💻 Command Output
intermediate
2:00remaining
What happens if set -x is turned off mid-script?
Given this bash script:
#!/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
A
+ var=10
var2=20
10 20
B
+ var=10
+ set +x
+ var2=20
+ echo 10 20
10 20
C
var=10
var2=20
10 20
D
+ var=10
var2=20
+ echo 10 20
10 20
Attempts:
2 left
💡 Hint
set +x disables trace mode, so commands after it won't show trace.
🔧 Debug
advanced
2:00remaining
Why does this script not show trace output after set -x?
Look at this script:
#!/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"
ABecause the if condition is false, the echo inside never runs, so no trace output.
BBecause set -x only traces commands outside if blocks.
CBecause set -x was disabled before the if block.
DBecause echo commands are never traced.
Attempts:
2 left
💡 Hint
Trace shows commands that actually run.
💻 Command Output
advanced
2:00remaining
What is the output of this script with nested set -x and set +x?
Analyze this script:
#!/bin/bash
set -x
var=1
{
  set +x
  var=2
  echo $var
}
echo $var

What is the full output including trace lines?
Bash Scripting
#!/bin/bash
set -x
var=1
{
  set +x
  var=2
  echo $var
}
echo $var
A
+ var=1
var=2
2
+ echo 2
2
B
+ var=1
var=2
2
+ echo 2
+ echo 2
1
C
+ var=1
var=2
2
+ echo 2
+ echo 2
2
D
+ var=1
var=2
2
+ echo 2
+ echo 2
+ echo 2
2
Attempts:
2 left
💡 Hint
set +x disables trace inside the block, but echo outside still traces.
🧠 Conceptual
expert
2: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?
AIt automatically fixes syntax errors in the script.
BIt shows each command and its arguments as they are executed, helping to trace script flow and spot errors.
CIt runs the script faster by optimizing commands.
DIt hides sensitive information from the output.
Attempts:
2 left
💡 Hint
Think about what trace mode does when enabled.

Practice

(1/5)
1. What does the set -x command do in a bash script?
easy
A. It enables trace mode, showing commands as they execute.
B. It disables all script output.
C. It pauses the script until user input.
D. It clears all variables in the script.

Solution

  1. Step 1: Understand the purpose of set -x

    The set -x command enables trace mode in bash, which prints each command and its arguments as they are executed.
  2. Step 2: Compare with other options

    Other options like disabling output, pausing, or clearing variables do not match the behavior of set -x.
  3. Final Answer:

    It enables trace mode, showing commands as they execute. -> Option A
  4. Quick Check:

    set -x shows commands [OK]
Hint: Remember: -x means eXecute trace ON [OK]
Common Mistakes:
  • Confusing set -x with set +x
  • Thinking it pauses the script
  • Assuming it clears variables
2. Which of the following is the correct syntax to turn off trace mode in a bash script?
easy
A. set -trace off
B. set -x
C. set +x
D. trace off

Solution

  1. Step 1: Recall how to disable trace mode

    Trace mode is enabled with set -x and disabled with set +x.
  2. Step 2: Check syntax correctness

    Options set -trace off and trace off are invalid bash commands.
  3. Final Answer:

    set +x -> Option C
  4. Quick Check:

    Disable trace with set +x [OK]
Hint: Use plus (+) to turn off options in bash [OK]
Common Mistakes:
  • Using set -x to turn off trace
  • Typing invalid commands like trace off
  • Confusing with other shell options
3. Consider this bash script snippet:
#!/bin/bash
set -x
var=5
let var=var+3
echo $var
set +x
echo "Done"
What will be the output when this script runs?
medium
A. 5 8 Done
B. Syntax error at let command
C. var=5 var=var+3 8 Done
D. + var=5 + let var=var+3 + echo 8 8 + set +x Done

Solution

  1. Step 1: Understand set -x effect

    With set -x, bash prints each command prefixed by '+', showing execution details.
  2. Step 2: Trace commands and output

    Commands var=5, let var=var+3, and echo $var print with '+'. The echo outputs '8'. After set +x, only 'Done' is printed normally.
  3. Final Answer:

    + var=5 + let var=var+3 + echo 8 8 + set +x Done -> Option D
  4. Quick Check:

    Trace mode shows commands with '+' [OK]
Hint: Trace mode prints commands with '+' before output [OK]
Common Mistakes:
  • Ignoring the '+' prefix in trace output
  • Thinking trace mode changes variable values
  • Assuming no command output is shown
4. You have this script snippet:
#!/bin/bash
set -x
for i in 1 2 3
  echo $i
done
set +x
What is the error and how to fix it?
medium
A. No error; script runs fine.
B. Missing 'do' after for loop; add 'do' before echo.
C. Missing quotes around $i; add quotes.
D. Incorrect use of set -x; remove it.

Solution

  1. Step 1: Identify syntax error in for loop

    The for loop syntax requires 'do' before the commands to run in the loop.
  2. Step 2: Fix the script

    Add 'do' after the for statement: for i in 1 2 3; do to fix the error.
  3. Final Answer:

    Missing 'do' after for loop; add 'do' before echo. -> Option B
  4. Quick Check:

    For loops need 'do' keyword [OK]
Hint: For loops always need 'do' before commands [OK]
Common Mistakes:
  • Forgetting 'do' in for loops
  • Removing set -x thinking it causes error
  • Adding quotes unnecessarily around loop variable
5. You want to debug only a part of your bash script using trace mode. Which is the best way to do this?
hard
A. Put set -x before the part to debug and set +x after it.
B. Use set -x at the start and never turn it off.
C. Add set +x before and set -x after the part to debug.
D. Use trace on and trace off commands.

Solution

  1. Step 1: Understand selective tracing

    To debug only a part, enable trace mode with set -x before that part and disable it with set +x after.
  2. Step 2: Evaluate other options

    Using set -x always shows too much output; set +x before and set -x after reverses logic; trace on/off are invalid commands.
  3. Final Answer:

    Put set -x before the part to debug and set +x after it. -> Option A
  4. Quick Check:

    Toggle trace with set -x and set +x [OK]
Hint: Turn trace on/off around code to limit debug output [OK]
Common Mistakes:
  • Leaving trace mode on for whole script
  • Using invalid commands like 'trace on'
  • Reversing set -x and set +x order
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