0
0
Linux CLIscripting~10 mins

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

Choose your learning style9 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.