Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 modes available
Concept Flow - set -e for exit on error
Start Script
↓
Run Command 1
↓
Command 1 Success?
No→Exit Script Immediately
Yes↓
Run Command 2
↓
Command 2 Success?
No→Exit 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'.

Practice

(1/5)
1. What does the set -e command do in a bash script?
easy
A. It stops the script immediately if any command returns a non-zero exit status.
B. It prints all commands before executing them.
C. It ignores errors and continues running the script.
D. It runs the script in debug mode.

Solution

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

    The set -e option tells bash to exit the script if any command fails (returns a non-zero status).
  2. Step 2: Compare with other options

    Options like printing commands or ignoring errors do not match set -e's behavior.
  3. Final Answer:

    It stops the script immediately if any command returns a non-zero exit status. -> Option A
  4. Quick Check:

    set -e = exit on error [OK]
Hint: Remember: set -e means exit on any error [OK]
Common Mistakes:
  • Confusing set -e with set -x (debug mode)
  • Thinking it ignores errors instead of stopping
  • Assuming it prints commands before running
2. Which of the following is the correct way to enable exit on error in a bash script?
easy
A. set -e
B. set +e
C. exit -e
D. enable -e

Solution

  1. Step 1: Recall the syntax for enabling exit on error

    The correct syntax to enable exit on error is set -e.
  2. Step 2: Eliminate incorrect options

    set +e disables exit on error, exit -e and enable -e are invalid commands.
  3. Final Answer:

    set -e -> Option A
  4. Quick Check:

    Enable exit on error = set -e [OK]
Hint: Use set -e to turn on exit on error [OK]
Common Mistakes:
  • Using set +e which disables exit on error
  • Confusing exit command with set
  • Typing invalid commands like enable -e
3. Consider this script with set -e enabled:
#!/bin/bash
set -e
echo "Start"
false
echo "End"
What will be the output when this script runs?
medium
A. Start End
B. No output
C. End
D. Start

Solution

  1. Step 1: Analyze script commands with set -e

    The script prints "Start", then runs false which returns a non-zero exit status causing the script to exit immediately.
  2. Step 2: Determine which commands run

    Because of set -e, the script stops after false, so "End" is never printed.
  3. Final Answer:

    Start -> Option D
  4. Quick Check:

    set -e stops script on error, so only "Start" prints [OK]
Hint: Script stops at first error, so later lines don't run [OK]
Common Mistakes:
  • Expecting all echo statements to run
  • Ignoring that false returns error
  • Thinking set -e allows script to continue
4. You have this script:
#!/bin/bash
set -e
echo "Start"
ls /nonexistent_directory
echo "End"
The script exits immediately after the ls command. How can you fix it to continue even if ls fails?
medium
A. Remove set -e from the script.
B. Add || true after the ls command.
C. Change ls to ls -e.
D. Add set +e at the start of the script.

Solution

  1. Step 1: Understand why script exits

    set -e causes the script to exit on any command failure, so ls /nonexistent_directory failing stops the script.
  2. Step 2: Use || true to ignore error

    Appending || true makes the command always succeed, preventing exit.
  3. Final Answer:

    Add || true after the ls command. -> Option B
  4. Quick Check:

    Use command || true to bypass set -e exit [OK]
Hint: Add || true to ignore errors on specific commands [OK]
Common Mistakes:
  • Removing set -e loses error protection
  • Using invalid ls -e option
  • Trying set +e disables exit globally, not selective
5. You want a bash script to stop on any error except for a specific command that might fail but should not stop the script. How do you write this script snippet correctly?
#!/bin/bash
set -e
# Your command here
other_command
Which snippet correctly allows your_command to fail without stopping the script?
hard
A. your_command ; true other_command
B. set +e your_command other_command
C. your_command || true other_command
D. trap '' ERR your_command other_command

Solution

  1. Step 1: Understand selective error ignoring

    To allow one command to fail without stopping the script under set -e, append || true to that command.
  2. Step 2: Compare other options

    your_command ; true other_command: your_command ; true fails because set -e exits after your_command before true. set +e your_command other_command disables set -e but doesn't re-enable it for other_command. trap '' ERR your_command other_command ignores ERR trap but doesn't restore it, affecting other_command.
  3. Final Answer:

    your_command || true other_command -> Option C
  4. Quick Check:

    Use command || true to ignore errors selectively [OK]
Hint: Add || true after command to ignore its errors [OK]
Common Mistakes:
  • Disabling set -e globally instead of locally
  • Using if without handling exit status properly
  • Using traps unnecessarily complicates script
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