set -e for exit on error in Bash Scripting - Time & Space Complexity
We want to understand how using set -e affects the running time of a bash script.
Specifically, does it change how long the script runs as input grows?
Analyze the time complexity of the following bash script snippet.
#!/bin/bash
set -e
for file in *.txt; do
grep "pattern" "$file"
done
This script searches for a pattern in all text files and stops immediately if any command fails.
Look for loops or repeated commands that affect execution time.
- Primary operation: The
forloop runninggrepon each file. - How many times: Once for each
.txtfile in the folder.
As the number of text files grows, the script runs grep more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 grep calls |
| 100 | About 100 grep calls |
| 1000 | About 1000 grep calls |
Pattern observation: The number of operations grows directly with the number of files.
Time Complexity: O(n)
This means the script's running time grows linearly with the number of files processed.
[X] Wrong: "Using set -e makes the script run faster because it stops early."
[OK] Correct: While set -e stops on errors, it does not speed up successful runs. The time still depends on how many files are processed before an error or completion.
Understanding how error handling affects script flow and time helps you write reliable scripts and explain your reasoning clearly in interviews.
What if we removed set -e and let the script continue even after errors? How would the time complexity change?
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
