Shell options (set -e, set -x) in Linux CLI - Time & Space Complexity
When using shell options like set -e and set -x, it is important to understand how they affect script execution time.
We want to see how these options change the way commands run and how long the script takes as it grows.
Analyze the time complexity of the following shell script snippet.
#!/bin/bash
set -e
set -x
for file in *.txt; do
grep "pattern" "$file"
done
This script stops on errors (set -e) and prints each command before running it (set -x), looping over text files to search for a pattern.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
forloop runs agrepcommand on each file. - How many times: Once for each
.txtfile in the folder.
As the number of .txt files grows, the script runs more grep commands and prints more lines due to set -x.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 grep runs and 10 command prints |
| 100 | About 100 grep runs and 100 command prints |
| 1000 | About 1000 grep runs and 1000 command prints |
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 it processes.
[X] Wrong: "Using set -x or set -e changes the loop's time complexity to something bigger like quadratic."
[OK] Correct: These options only add extra output or stop on errors but do not add nested loops or repeated work inside the loop, so the time still grows linearly.
Understanding how shell options affect script execution helps you write reliable scripts and explain their performance clearly in real work or interviews.
"What if we added a nested loop inside the for loop to process each file's lines? How would the time complexity change?"
Practice
set -e do in a script?Solution
Step 1: Understand the purpose of
This option makes the shell stop running the script if any command fails (returns a non-zero exit code).set -eStep 2: Compare with other options
set -xprints commands, butset -estops on errors, so stopping on error is correct.Final Answer:
Stops the script immediately if any command returns an error -> Option AQuick Check:
set -estops on error [OK]
- Confusing
set -ewithset -x - Thinking it ignores errors
- Assuming it runs commands silently
set -e and set -x in a shell script?Solution
Step 1: Understand combined options syntax
Shell options can be combined after a single dash, order does not matter, soset -xeworks.Step 2: Check options with spaces
set e -xis invalid syntax, andset -x eorset -e xset only one option.Final Answer:
set -xe -> Option BQuick Check:
Combine options after one dash [OK]
- Using spaces between flags incorrectly
- Writing flags without dashes
- Assuming order matters
set -e enabled:
set -e mkdir /tmp/mydir cd /tmp/mydir false echo "This will not print"What will be the output when running this script?
Solution
Step 1: Trace commands with
The script creates /tmp/mydir, changes into it, then runsset -efalsewhich returns error (non-zero).Step 2: Effect of
Becauseset -eon errorset -estops on any error, the script stops afterfalseand does not run the echo command.Final Answer:
Creates directory, changes directory, then stops at false; no echo output -> Option CQuick Check:
set -estops script on error [OK]
- Assuming echo runs after error
- Thinking mkdir fails by default
- Ignoring effect of set -e
#!/bin/bash set -x ls /nonexistent pwdWhat is the problem and how to fix it so the script continues after the error?
Solution
Step 1: Analyze script options
The script usesset -xwhich prints commands but does not stop on errors. There is noset -eso script continues despitelserror and runspwd.Step 2: Identify problem and fix
Script already continues after error since noset -e. No problem exists.Final Answer:
No problem; script runs all commands -> Option AQuick Check:
set -xprints commands,set -estops on error [OK]
set -x prints, set -e stops [OK]- Confusing
set -xwithset -e - Assuming
set -xstops script - Ignoring missing
set -e
Solution
Step 1: Understand debugging needs
To debug, you want to see commands as they run (set -x) and stop on errors (set -e).Step 2: Combine options correctly
set -exenables both correctly.Final Answer:
set -ex -> Option DQuick Check:
Show commands and stop on error = set -ex [OK]
- Using only one option and missing the other
- Thinking order matters strictly
- Confusing which option stops or prints
Ready to prove it?
Pass the test and this pattern moves unknown
