How to Check Exit Status for Command in Linux

When working on a Linux system, every command you run returns a status code (also called an exit status or return code) that tells you whether it ran successfully or failed.

As a Linux user, especially if you’re learning shell scripting or troubleshooting, knowing how to check the exit status of a command is super important.

In this article, I’ll explain what exit status means, how to check it, and why it matters.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

What is Exit Status in Linux?

Whenever you run a command in Linux, the system quietly gives it a scorecard at the end, which is just a number called the exit status (or return code).

  • If the number is 0, it means, the command ran successfully, without any errors.
  • If the number is not 0, it means, the command failed to run in some way.

Think of it like this:

  • 0 = “All good!”
  • 1 = “Something went wrong (general error)”
  • 2, 127, 126, etc. = Different kinds of problems, like “file not found” or “command not executable“.

You don’t always see these numbers printed on the screen, but Linux stores them in a special variable called $?. You can check it anytime to know whether your last command worked or failed.

Tip: These exit codes are especially important when you write Bash scripts because they help you make decisions depending on whether a command passed or failed.

How to Check Exit Status of a Command

In Linux, the exit status of the last command you ran is stored in a special shell variable called:

$?

The best way to understand exit status is by actually running commands.

Example 1: Successful Command

ls
echo $?

What happens here:

  • First, ls lists the files in your current directory.
  • Since the command worked without any issue, Linux sets the exit status to 0.
  • When we immediately check the status using echo $?, it prints 0.
Check Command Exit Status in Linux
Check Command Exit Status in Linux

A 0 exit code always means success. So in this case, the ls command did exactly what we expected.

Example 2: Failed Command

ls /nonexistent
echo $?

What happens here:

  • We tried to list files inside a directory called /nonexistent.
  • That directory does not exist on the system, so the ls command fails.
  • Linux then sets the exit status to a non-zero number (in this case 2).
  • When we check with echo $?, we see:
Check Exit Status of Failed Commands in Linux
Check Exit Status of Failed Commands in Linux

Example 3: Using Exit Status in a Script

Exit statuses become really useful when you use them inside shell scripts, because scripts often need to make decisions based on whether a command succeeded or failed.

Let’s look at a simple example:

#!/bin/bash

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

What happens here:

  • If the ls command works → prints “Command successful!”.
  • If it fails → prints “Command failed!”.

A Cleaner Way: Using && and ||

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

  • command && echo "Success" → runs the second command only if the first succeeded.
  • command || echo "Failed" → runs the second command only if the first failed.

Example:

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

Common Exit Status Codes

Here are some frequently seen exit codes:

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
Final Thoughts

The exit status is a small number, but it plays a big role in running commands interactively or writing shell scripts, understanding exit codes helps you debug problems and control your workflow.

Next time a command doesn’t work as expected, don’t just look at the error message, just check the exit status too!

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Ravi Saive
I'm Ravi Saive, an award-winning entrepreneur and founder of several successful 5-figure online businesses, including TecMint.com, GeeksMint.com, UbuntuMint.com, and the premium learning hub Pro.Tecmint.com.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.