How to Check Exit Status in Linux Commands (Bash Examples)

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 $?
Output:
0

he ls command worked, so exit code = 0.

Example 2 – Failed Command

ls /nonexistent
echo $?
Output:
2
Exit code 2 indicates a failure because the directory does not exist.

Using Exit Status in Scripts

Exit statuses are extremely useful in Bash scripting.

#!/bin/bash

ls /etc > /dev/null
if [ $? -eq 0 ]; then
echo "Command successful!"
else
echo "Command failed!"
fi

If the ls command succeeds → prints “Command successful!”.

If it fails → prints “Command failed!”.

A Cleaner Way: && and || Operators

Instead of checking $? every time, you can chain commands:

  • Run only if success:

command && echo "Success"

Run only if fail:

command || echo "Failed"

Example:

ls /etc && echo "Found it!" || echo "Not found!"

Common Exit Status Codes in Linux

Exit CodeMeaning
0Success
1General error
2Misuse of shell command
126Command found but not executable
127Command not found
130Script terminated by Ctrl+C
255Exit 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:

check_service() {
    systemctl status sshd > /dev/null 2>&1
    return $?
}

check_service
if [ $? -eq 0 ]; then
    echo "SSH is running"
else
    echo "SSH is not running"
fi

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:

echo $?

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:

Share this:
WhatsApp Channel Join Now
Telegram Channel Join Now
Instagram Channel Join Now

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.