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.
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 prints0
.

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:

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!