0
0
Bash Scriptingscripting~10 mins

set -e for exit on error in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - set -e for exit on error
Start Script
Run Command 1
Command 1 Success?
NoExit Script Immediately
Yes
Run Command 2
Command 2 Success?
NoExit Script Immediately
Yes
Continue Running Commands
End Script
The script runs commands one by one. If any command fails, the script stops immediately.
Execution Sample
Bash Scripting
set -e

mkdir testdir
cd testdir
false
echo "This will not run"
This script creates a directory, changes into it, runs a failing command, then tries to echo text (which won't run).
Execution Table
StepCommandExit StatusActionOutput
1set -e0Enable exit on error
2mkdir testdir0Create directory
3cd testdir0Change directory
4false1Command failed, exit script
5echo "This will not run"N/ANot executed due to previous failure
💡 Script exits immediately after 'false' command returns non-zero status due to 'set -e'
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Current Directory$PWD$PWD/testdir$PWD/testdir$PWD/testdir$PWD/testdir
Key Moments - 2 Insights
Why does the script stop after the 'false' command?
Because 'set -e' makes the script exit immediately when any command returns a non-zero status, as shown in step 4 of the execution table.
Will the echo command run after the 'false' command?
No, the echo command is not executed because the script exits at step 4 due to the failure, as indicated in the execution table row 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the exit status of the 'mkdir testdir' command at step 2?
A0
B1
CCommand not run
DUnknown
💡 Hint
Check the 'Exit Status' column for step 2 in the execution table.
At which step does the script exit due to an error?
AStep 3
BStep 4
CStep 5
DStep 2
💡 Hint
Look for the step where the 'Exit Status' is non-zero and the action says 'exit script'.
If 'set -e' was not used, what would happen to the echo command at step 5?
AIt would run and print the message
BIt would not run
CScript would exit before step 5 anyway
DIt would cause an error
💡 Hint
Without 'set -e', the script continues even if a command fails, so step 5 runs.
Concept Snapshot
set -e enables exit on error in bash scripts
If any command fails (non-zero exit), script stops immediately
Use to avoid running commands after failures
Place 'set -e' at script start
Useful for safer automation
Full Transcript
This visual execution shows how 'set -e' works in bash scripts. The script starts and runs commands one by one. When a command fails, like 'false' returning exit status 1, the script stops immediately. Commands after the failure do not run. This helps prevent errors from cascading. The variable tracker shows the current directory changes. Key moments clarify why the script stops and why later commands don't run. The quiz tests understanding of exit status and script flow with 'set -e'.