What if your script could warn you the moment something breaks, saving hours of frustration?
Why Shell options (set -e, set -x) in Linux CLI? - Purpose & Use Cases
Imagine you run a long script on your computer to backup files and update software. You watch the screen, hoping nothing goes wrong. But if a command fails, the script keeps running, causing confusion and wasted time.
Manually checking each command's success is slow and easy to forget. Without clear feedback, errors hide and cause bigger problems later. You might spend hours finding where things went wrong.
Using shell options like set -e stops the script immediately when a command fails, saving time and preventing mistakes. set -x shows each command as it runs, so you can see exactly what's happening step-by-step.
command1 command2 command3
set -e set -x command1 command2 command3
It lets you catch errors early and understand your script's actions clearly, making automation safer and easier.
When deploying a website update, set -e stops the process if a file fails to copy, preventing a broken site. set -x helps you see each step, so you know exactly what happened if something goes wrong.
Stop errors early: set -e halts on failure to avoid hidden problems.
See commands run: set -x prints commands for easy debugging.
Save time and stress: These options make scripts safer and clearer.
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
