📖 Scenario: You are writing a simple bash script to run a few commands in order. You want the script to stop immediately if any command fails, so you don't continue with wrong results.
🎯 Goal: Learn how to use set -e in a bash script to make it exit on the first error.
📋 What You'll Learn
Create a bash script with three commands
Add set -e to make the script exit on error
Test the script to see it stops when a command fails
Print a message after the commands to confirm script behavior
💡 Why This Matters
🌍 Real World
In real scripts, <code>set -e</code> helps catch errors early and prevents running commands that depend on previous successful steps.
💼 Career
Many jobs require writing reliable bash scripts for automation. Knowing how to use <code>set -e</code> is essential for safe scripting.
Progress0 / 4 steps
1
Create a bash script with three commands
Create a bash script with these three commands exactly: echo "Start script", ls /nonexistentfolder, and echo "End script".
Bash Scripting
Hint
Use echo to print messages and ls to list a folder that does not exist.
2
Add set -e to exit on error
Add the line set -e at the very top of the script to make it exit immediately if any command fails.
Bash Scripting
Hint
set -e must be the first line in the script.
3
Add a success message after commands
Add a new line at the end of the script that prints echo "Script completed successfully".
Bash Scripting
Hint
This message will only print if no command fails before it.
4
Run the script and observe output
Run the script and observe that it prints Start script, then stops with an error because ls /nonexistentfolder fails. It should NOT print End script or Script completed successfully. Use bash yourscript.sh to run.
Bash Scripting
Hint
The script stops after the error. You will see the error message from ls.
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
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).
Step 2: Compare with other options
Options like printing commands or ignoring errors do not match set -e's behavior.
Final Answer:
It stops the script immediately if any command returns a non-zero exit status. -> Option A
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
Step 1: Recall the syntax for enabling exit on error
The correct syntax to enable exit on error is set -e.
Step 2: Eliminate incorrect options
set +e disables exit on error, exit -e and enable -e are invalid commands.
Final Answer:
set -e -> Option A
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
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.
Step 2: Determine which commands run
Because of set -e, the script stops after false, so "End" is never printed.
Final Answer:
Start -> Option D
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
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.
Step 2: Use || true to ignore error
Appending || true makes the command always succeed, preventing exit.
Final Answer:
Add || true after the ls command. -> Option B
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
Step 1: Understand selective error ignoring
To allow one command to fail without stopping the script under set -e, append || true to that command.
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.
Final Answer:
your_command || true
other_command -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.