Error Handling & Exit Status
Implement robust error checking in your scripts by understanding exit statuses and using 'set -e' and conditional logic.
Error Handling & Exit Status is a free DevOps Bootcamp lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the DevOps Bootcamp learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Welcome to Error Handling
In this lesson, we'll make your Bash scripts more robust by learning how to handle errors effectively.
Imagine a script that performs many steps. If one step fails, you'd want your script to stop or at least know about the failure, right? That's where error handling comes in!
What is an Exit Status?
Every command you run in the Linux shell returns an exit status (sometimes called an exit code or return code).
- A status of 0 means the command completed successfully.
- A non-zero status (e.g., 1, 2, 127) indicates some form of failure or error.
It's like a traffic light for your commands!
Checking Exit Status Manually
You can check the exit status of the last executed command using the special variable $?.
Let's try a successful command and a failing one:
#!/bin/bash
echo "Hello CoddyKit"
echo "Exit status: $?"
ls non_existent_file.txt
echo "Exit status: $?"Basic Conditional Error Check
Knowing the exit status allows your script to make decisions. You can use an if statement to check $? and react to errors.
This makes your script aware of problems and prevents it from continuing with invalid data.
#!/bin/bash
ls non_existent_file.txt
if [ $? -ne 0 ]; then
echo "Error: The 'ls' command failed!"
exit 1 # Exit script with an error code
fi
echo "This line will not run if 'ls' fails."The Power of `set -e`
Manually checking $? after every command can be tedious. This is where set -e comes in handy!
When you start a script with set -e, Bash will immediately exit if any command (that isn't part of a conditional test) exits with a non-zero status.
It's a global safety switch for your scripts!
`set -e` in Action
Let's see set -e prevent a script from continuing after an error. Notice how the final echo command is never reached.
#!/bin/bash
set -e
echo "Starting important tasks..."
# This command will fail
cp /non_existent_source /tmp/destination
echo "This line will NOT be printed if 'cp' fails."
echo "All tasks completed successfully."When `set -e` Doesn't Stop
set -e is powerful, but it has specific rules. It will not cause the script to exit if the failing command is:
- Part of an
iforwhilecondition. - Part of an
&&or||list (except the last command). - Preceded by
!.
These contexts are designed to test for success/failure, so `set -e` doesn't interfere.
Combining `set -e` with `||`
Sometimes you want a custom error message before exiting. You can combine set -e with the || (OR) operator.
command || { echo "Error message"; exit 1; }
If command fails, the part after || executes, prints your message, and then exits with a specific error code.
#!/bin/bash
set -e
echo "Attempting to find a file..."
find /non_existent_dir -name "*.txt" || \
{ echo "ERROR: Could not search in /non_existent_dir!"; exit 1; }
echo "Script continued (this won't print if 'find' fails)."Handling Specific Exit Codes
While 0 means success and non-zero means failure, specific non-zero codes can have different meanings.
1: General error.2: Misuse of shell builtins.127: Command not found.
You can use if [ $? -eq 127 ] to check for specific failure types, offering more tailored error feedback.
Error Handling Quiz
Which of the following statements about Bash exit statuses and error handling is TRUE?
Recap: Robust Scripts
You've learned how to make your Bash scripts much more reliable!
- Every command returns an exit status (0 for success, non-zero for failure).
- The
$?variable gives you the last command's exit status. set -eis a powerful tool that automatically exits your script on most command failures.- Combine
set -ewith||for custom error messages.
Using these techniques will help you write more professional and stable scripts.
Frequently asked questions
Is the “Error Handling & Exit Status” lesson free?
Yes — the full text of “Error Handling & Exit Status” is free to read here on the web, and the DevOps Bootcamp course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the DevOps Bootcamp course, upgrade to CoddyKit PRO.
What will I learn in “Error Handling & Exit Status”?
Implement robust error checking in your scripts by understanding exit statuses and using 'set -e' and conditional logic. You practise DevOps Bootcamp with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start DevOps Bootcamp?
No prior experience is required. DevOps Bootcamp on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Error Handling & Exit Status” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this DevOps Bootcamp lesson?
Yes. Every DevOps Bootcamp lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Debugging Bash Scripts (set -x, trap)
- Error Handling & Exit Status
- Scripting Best Practices & Linting
- Testing Bash Scripts with Bats