Bird
Raised Fist0
Linux CLIscripting~10 mins

Shell options (set -e, set -x) in Linux CLI - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Shell options (set -e, set -x)
Start Script
↓
Set options: set -e, set -x
↓
Execute commands one by one
↓
If set -x: Print each command before running
↓
If command fails and set -e is on: Exit script immediately
↓
End Script or Exit on error
The shell script starts, sets options to print commands and exit on errors, then runs commands step-by-step, stopping if any command fails.
Execution Sample
Linux CLI
set -e
set -x
echo "Start"
false
echo "End"
This script prints commands as they run, stops immediately when a command fails (false), so 'End' is never printed.
Execution Table
StepCommandPrinted (set -x)Command ResultAction TakenOutput
1set -eSuccessEnable exit on error
2set -xSuccessEnable command printing
3echo "Start"+ echo "Start"SuccessPrints StartStart
4false+ falseFailureExit script immediately due to set -e
5echo "End"Not executedScript exited before this
💡 Script exits at step 4 because 'false' command fails and set -e is enabled
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
set -eoffononononon
set -xoffoffonononon
Script Runningyesyesyesyesnono
Key Moments - 2 Insights
Why does the script stop running after the 'false' command?
Because set -e is enabled (see step 1 in execution_table), the script exits immediately when a command fails, which happens at step 4 with 'false'.
Why do we see the commands printed before their output?
Because set -x is enabled at step 2, the shell prints each command before running it, as shown in the 'Printed (set -x)' column in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed at step 3 before the output?
Afalse
BStart
Cecho "Start"
Dset -e
💡 Hint
Check the 'Printed (set -x)' column at step 3 in the execution_table.
At which step does the script stop running due to an error?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action Taken' column where the script exits due to failure.
If we remove 'set -e', what would happen at step 4?
AScript would continue to step 5
BScript would exit immediately
CCommands would not be printed
DScript would print an error and pause
💡 Hint
Refer to the 'set -e' variable state in variable_tracker and its effect on script flow.
Concept Snapshot
Shell options control script behavior:
- set -e: Exit script on any command failure
- set -x: Print each command before executing
Use set -e to catch errors early
Use set -x to debug by seeing commands run
Together they help write safer, easier-to-debug scripts
Full Transcript
This visual trace shows how shell options set -e and set -x affect script execution. The script starts by enabling set -e to exit on errors and set -x to print commands. Each command runs in order: 'echo "Start"' prints 'Start'. Then 'false' runs and fails. Because set -e is on, the script stops immediately and does not run 'echo "End"'. The variable tracker shows set -e and set -x states turning on and the script running status changing to no after failure. Key moments clarify why the script stops on error and why commands print before output. The quiz tests understanding of printed commands, exit step, and effect of removing set -e.

Practice

(1/5)
1. What does the shell option set -e do in a script?
easy
A. Stops the script immediately if any command returns an error
B. Prints each command before executing it
C. Ignores all errors and continues running
D. Runs the script in the background

Solution

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

    This option makes the shell stop running the script if any command fails (returns a non-zero exit code).
  2. Step 2: Compare with other options

    set -x prints commands, but set -e stops on errors, so stopping on error is correct.
  3. Final Answer:

    Stops the script immediately if any command returns an error -> Option A
  4. Quick Check:

    set -e stops on error [OK]
Hint: Remember: 'e' for error stop [OK]
Common Mistakes:
  • Confusing set -e with set -x
  • Thinking it ignores errors
  • Assuming it runs commands silently
2. Which of the following is the correct way to enable both set -e and set -x in a shell script?
easy
A. set -x e
B. set -xe
C. set e -x
D. set -e x

Solution

  1. Step 1: Understand combined options syntax

    Shell options can be combined after a single dash, order does not matter, so set -xe works.
  2. Step 2: Check options with spaces

    set e -x is invalid syntax, and set -x e or set -e x set only one option.
  3. Final Answer:

    set -xe -> Option B
  4. Quick Check:

    Combine options after one dash [OK]
Hint: Combine flags after one dash, order doesn't matter [OK]
Common Mistakes:
  • Using spaces between flags incorrectly
  • Writing flags without dashes
  • Assuming order matters
3. Given this script snippet with set -e enabled:
set -e
mkdir /tmp/mydir
cd /tmp/mydir
false
echo "This will not print"
What will be the output when running this script?
medium
A. Fails at mkdir and stops immediately
B. Creates directory, changes directory, prints 'This will not print'
C. Creates directory, changes directory, then stops at false; no echo output
D. Runs all commands including echo

Solution

  1. Step 1: Trace commands with set -e

    The script creates /tmp/mydir, changes into it, then runs false which returns error (non-zero).
  2. Step 2: Effect of set -e on error

    Because set -e stops on any error, the script stops after false and does not run the echo command.
  3. Final Answer:

    Creates directory, changes directory, then stops at false; no echo output -> Option C
  4. Quick Check:

    set -e stops script on error [OK]
Hint: Remember: script stops at first error with set -e [OK]
Common Mistakes:
  • Assuming echo runs after error
  • Thinking mkdir fails by default
  • Ignoring effect of set -e
4. You have this script:
#!/bin/bash
set -x
ls /nonexistent
pwd
What is the problem and how to fix it so the script continues after the error?
medium
A. No problem; script runs all commands
B. Problem: script stops on error; fix by removing set -x
C. Problem: script prints commands; fix by removing set -x
D. Problem: script stops on error; fix by removing set -e (not set here)

Solution

  1. Step 1: Analyze script options

    The script uses set -x which prints commands but does not stop on errors. There is no set -e so script continues despite ls error and runs pwd.
  2. Step 2: Identify problem and fix

    Script already continues after error since no set -e. No problem exists.
  3. Final Answer:

    No problem; script runs all commands -> Option A
  4. Quick Check:

    set -x prints commands, set -e stops on error [OK]
Hint: Remember: set -x prints, set -e stops [OK]
Common Mistakes:
  • Confusing set -x with set -e
  • Assuming set -x stops script
  • Ignoring missing set -e
5. You want to debug a script that sometimes fails silently. Which combination of shell options helps you see each command as it runs and stops immediately on errors?
hard
A. set -e only
B. set -x e
C. set -x only
D. set -ex

Solution

  1. Step 1: Understand debugging needs

    To debug, you want to see commands as they run (set -x) and stop on errors (set -e).
  2. Step 2: Combine options correctly

    set -ex enables both correctly.
  3. Final Answer:

    set -ex -> Option D
  4. Quick Check:

    Show commands and stop on error = set -ex [OK]
Hint: Use both -e and -x together to debug safely [OK]
Common Mistakes:
  • Using only one option and missing the other
  • Thinking order matters strictly
  • Confusing which option stops or prints
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