0
0
Bash Scriptingscripting~5 mins

set -x for trace mode in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does set -x do in a bash script?

set -x turns on trace mode, which shows each command and its arguments as they are executed. It helps you see what the script is doing step-by-step.

Click to reveal answer
beginner
How do you turn off trace mode after using set -x?

You use set +x to stop showing commands in trace mode.

Click to reveal answer
beginner
Why is set -x useful when writing bash scripts?

It helps you find mistakes by showing exactly what commands run and in what order, like watching the script work live.

Click to reveal answer
beginner
Show a simple example of using set -x in a bash script.
#!/bin/bash set -x name="Alice" echo "Hello, $name" set +x echo "Done"

This script shows commands as they run until set +x.

Click to reveal answer
intermediate
Can set -x slow down your script? Why or why not?

Yes, because it prints every command to the screen, which takes extra time and can clutter output.

Click to reveal answer
What command turns on trace mode in bash?
Aset +x
Btrace on
Cset -x
Ddebug start
How do you stop trace mode in a bash script?
Aset +x
Bset -x
Cstop trace
Dunset trace
What is the main benefit of using set -x?
AHide commands from output
BSee commands as they run for debugging
CSpeed up script execution
DAutomatically fix errors
Which of these is a side effect of using set -x?
AOutput becomes cluttered
BCommands are hidden
CScript runs faster
DScript stops on errors
Where in a script should you place set +x?
ABefore <code>set -x</code>
BAt the very end only
CAt the very start
DAfter you want to stop tracing
Explain how set -x helps when debugging a bash script.
Think about how seeing each command helps you understand what the script does.
You got /4 concepts.
    Describe how to use set -x and set +x in a script to control tracing.
    Consider where you want to see commands and where you want normal output.
    You got /4 concepts.