What if your script could stop itself the moment something goes wrong, saving you hours of troubleshooting?
Why set -e for exit on error in Bash Scripting? - Purpose & Use Cases
Imagine you are running a long script to install software and configure settings. You type each command one by one, hoping everything works perfectly. But if one command fails, the script keeps running, causing confusing errors later.
Manually checking each command's success is slow and easy to forget. If a command fails, the script blindly continues, making it hard to find where things went wrong. This wastes time and can break your system setup.
Using set -e tells the script to stop immediately when any command fails. This way, you catch errors early, avoid running bad commands, and save time debugging.
command1 command2 command3
set -e command1 command2 command3
You can trust your scripts to stop on errors, making automation safer and easier to maintain.
When deploying a website, if a step like copying files fails, set -e stops the process immediately, preventing a broken or incomplete deployment.
Manual scripts can run past errors, causing bigger problems.
set -e stops the script on the first error.
This makes scripts more reliable and easier to fix.
Practice
set -e command do in a bash script?Solution
Step 1: Understand the purpose of
Theset -eset -eoption 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 matchset -e's behavior.Final Answer:
It stops the script immediately if any command returns a non-zero exit status. -> Option AQuick Check:
set -e= exit on error [OK]
set -e means exit on any error [OK]- Confusing
set -ewithset -x(debug mode) - Thinking it ignores errors instead of stopping
- Assuming it prints commands before running
Solution
Step 1: Recall the syntax for enabling exit on error
The correct syntax to enable exit on error isset -e.Step 2: Eliminate incorrect options
set +edisables exit on error,exit -eandenable -eare invalid commands.Final Answer:
set -e -> Option AQuick Check:
Enable exit on error =set -e[OK]
set -e to turn on exit on error [OK]- Using
set +ewhich disables exit on error - Confusing
exitcommand withset - Typing invalid commands like
enable -e
set -e enabled:
#!/bin/bash set -e echo "Start" false echo "End"What will be the output when this script runs?
Solution
Step 1: Analyze script commands with
The script prints "Start", then runsset -efalsewhich returns a non-zero exit status causing the script to exit immediately.Step 2: Determine which commands run
Because ofset -e, the script stops afterfalse, so "End" is never printed.Final Answer:
Start -> Option DQuick Check:
set -estops script on error, so only "Start" prints [OK]
- Expecting all echo statements to run
- Ignoring that
falsereturns error - Thinking
set -eallows script to continue
#!/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?Solution
Step 1: Understand why script exits
set -ecauses the script to exit on any command failure, sols /nonexistent_directoryfailing stops the script.Step 2: Use
Appending|| trueto ignore error|| truemakes the command always succeed, preventing exit.Final Answer:
Add|| trueafter thelscommand. -> Option BQuick Check:
Usecommand || trueto bypassset -eexit [OK]
|| true to ignore errors on specific commands [OK]- Removing
set -eloses error protection - Using invalid
ls -eoption - Trying
set +edisables exit globally, not selective
#!/bin/bash set -e # Your command here other_commandWhich snippet correctly allows
your_command to fail without stopping the script?Solution
Step 1: Understand selective error ignoring
To allow one command to fail without stopping the script underset -e, append|| trueto that command.Step 2: Compare other options
your_command ; true other_command:your_command ; truefails becauseset -eexits afteryour_commandbeforetrue. set +e your_command other_command disablesset -ebut doesn't re-enable it forother_command. trap '' ERR your_command other_command ignores ERR trap but doesn't restore it, affectingother_command.Final Answer:
your_command || true other_command -> Option CQuick Check:
Usecommand || trueto ignore errors selectively [OK]
|| true after command to ignore its errors [OK]- Disabling
set -eglobally instead of locally - Using if without handling exit status properly
- Using traps unnecessarily complicates script
Ready to prove it?
Pass the test and this pattern moves unknown
