When working with Linux, every command you run produces a special numeric result called an exit status or return code. This number quietly tells you whether the command was executed successfully or if it failed. Knowing how to check exit status in Linux is an essential skill for system administrators, developers, and anyone learning Bash scripting.
In this detailed guide, you’ll learn what exit statuses are, how to check them, and how they are used in troubleshooting and automation. We’ll also look at common error codes, scripting examples, and advanced techniques.
What is Exit Status in Linux?
Whenever a command finishes execution, Linux assigns it a small integer called the exit status (or return code).
0 → Command executed successfully.
Non-zero → Command failed in some way.
Think of it as a “scorecard”:
0 = All good
1 = General error
127 = Command not found
126 = Command not executable
This exit status is not printed automatically, but Linux stores it in a special shell variable called $?
. You can check it anytime to determine the result of your last executed command.
Why Do You Need to Check Exit Status in Linux?
Here are some important reasons:
Troubleshooting – Understand why a command failed.
Scripting – Automate decision-making based on success or failure.
Monitoring – Detect crashes or failures in scripts and services.
Debugging – Learn more than just error messages by checking exit codes.
If you don’t check exit status in Linux, you may miss the root cause of a problem.
How to Check Exit Status in Linux
Using $?
Variable
The simplest way is to use the special variable $?
immediately after running a command.
Example 1 – Successful Command
ls echo $?
0
he ls
command worked, so exit code = 0.
Example 2 – Failed Command
ls /nonexistent echo $?
Using Exit Status in Scripts
Exit statuses are extremely useful in Bash scripting.
Run only if fail:
Example:
Common Exit Status Codes in Linux
Exit Code | Meaning |
---|---|
0 | Success |
1 | General error |
2 | Misuse of shell command |
126 | Command found but not executable |
127 | Command not found |
130 | Script terminated by Ctrl+C |
255 | Exit status out of range |
Advanced Usage: Exit Status in Complex Scripts
Sometimes, you need multiple conditions:
#!/bin/bash ping -c 1 google.com > /dev/null 2>&1 if [ $? -eq 0 ]; then echo "Internet is available" else echo "No connectivity" fi
Or using a function:
This is a real-world example where checking exit status in Linux becomes critical.
Exit Status and Automation
Cron Jobs – Log exit codes of scheduled jobs to monitor failures.
CI/CD Pipelines – Use exit status to stop deployment if a test fails.
System Monitoring – Combine with Nagios/Zabbix scripts to detect service failures.
Example logging script:
#!/bin/bash mycommand if [ $? -ne 0 ]; then echo "$(date): mycommand failed" >> /var/log/custom_errors.log fi
Best Practices
Always check
$?
after critical commands.Use
&&
and||
for cleaner syntax.Redirect errors (
2>&1
) when debugging.In scripts, exit with meaningful codes (
exit 1
,exit 2
, etc.) for clarity.
Summary
Every command in Linux returns an exit status.
0
= success, non-zero = failure.Use
$?
to check exit status in Linux.In Bash scripts, use conditions,
&&
,||
, and functions.Learn common exit codes like
126
,127
,130
.Use automation and logging for system reliability.
Conclusion
Learning to check exit status in Linux is essential for shell scripting, troubleshooting, and automation. Instead of guessing why a command failed, use exit codes to diagnose issues quickly. Whether you are writing scripts or managing servers, exit statuses help ensure your workflow is efficient, reliable, and error-free.
FAQs
Q1. How do I check exit status in Linux?
Use:
right after running a command.
Q2. What does exit code 0 mean in Linux?
Exit code 0
always means success.
Q3. What is the difference between exit code 126 and 127?
126
→ Command found but not executable.127
→ Command not found.
Q4. Can I use exit status in Bash scripts?
Yes. Scripts often use $?
, &&
, ||
to decide the next step.
Q5. Why is checking exit status in Linux important?
It helps with debugging, automation, and making reliable scripts.
Suggested Read:
- How to Auto Execute Commands During Startup in Linux
- How to Kill a Process (Terminate a running process) in Linux
- How Linux handles Processes
- How to manipulate Process Priority in Linux using nice and renice Commands
- Linux Networking Commands for Sysadmins: A Complete Guide
- Most Useful Linux Ping Command (Ping Utility) With Examples
- Best Linux Ifconfig Command With Examples
- How To Install Wireshark Network Analyzer In Ubuntu – A Best Network Traffic Analyzer For Linux
- Setup Netdata For Real Time Performance Monitor In Linux System