What is the difference between exit code 0 and a non-zero exit code in Linux?
Understand Linux exit codes: why 0 means success and non-zero means failure, how to read $?, and how special codes like 127 and 128+N power shell scripting.
Expected Interview Answer
In Linux, an exit code of 0 means the command succeeded, while any non-zero exit code (1 to 255) signals failure, with the specific number indicating the kind of error.
Every process returns an exit status when it terminates, and the shell exposes it as $?. By convention 0 is success and non-zero is failure, letting scripts branch on results. Specific ranges carry meaning: 1 is a generic error, 2 is misuse of a shell builtin, 126 means found but not executable, 127 means command not found, and 128+N means the process was killed by signal N. This convention powers &&, || and if in shell scripting.
- Lets scripts detect success or failure automatically
- Enables && and || conditional chaining
- Specific codes distinguish error types
- CI/CD pipelines gate on the exit status
- Makes error handling explicit and testable
AI Mentor Explanation
An exit code of 0 is like a clean, legal delivery the umpire signals nothing for, meaning play proceeds smoothly. A non-zero code is any signal the umpire raises, a wide, a no-ball, or an out, and the exact signal tells everyone precisely what went wrong so the scorers and captains react correctly to that specific event.
Step-by-Step Explanation
Step 1
Run a command
Execute any command; the shell captures its termination status automatically.
Step 2
Read $?
Inspect the previous command's exit code with echo $? immediately after it finishes.
Step 3
Interpret the value
0 means success; 1-255 mean failure, with 127 for not-found and 128+N for signal N.
Step 4
Branch on it
Use if cmd; then ... fi or cmd && next or cmd || fallback to act on success or failure.
Step 5
Set your own
In scripts, call exit N to return a meaningful status to whatever invoked the script.
What Interviewer Expects
- Knowing 0 means success and non-zero means failure
- Reading the status via $?
- Awareness of special codes like 127 and 128+N
- Using exit codes with &&, || and if
- Setting exit codes in your own scripts
Common Mistakes
- Thinking non-zero means success like a boolean true
- Checking $? after another command has already overwritten it
- Ignoring exit codes entirely in scripts
- Confusing exit codes with stdout output
- Not knowing 127 means command not found
Best Answer (HR Friendly)
“When a Linux command finishes it returns a number: zero means it worked, and any other number means something went wrong. Scripts read that number to decide what to do next, like retrying or stopping.”
Code Example
ls /tmp
echo $? # 0 on success
ls /nope 2>/dev/null
echo $? # non-zero (e.g. 2) on failure
# Branch on success/failure
if grep -q root /etc/passwd; then
echo "found"
else
echo "missing"
fi
# Chain: run second only if first succeeds
mkdir build && cd build
# Set a custom status in a script
exit 3Follow-up Questions
- What does exit code 127 typically indicate?
- How is a process killed by SIGKILL reflected in its exit code?
- How do && and || use exit codes to chain commands?
- Why should you capture $? before running any other command?
MCQ Practice
1. What does an exit code of 0 mean in Linux?
By convention 0 indicates success; any non-zero value indicates some form of failure.
2. Which variable holds the last command's exit code?
$? expands to the exit status of the most recently completed foreground command.
3. An exit code of 127 usually means what?
127 conventionally signals that the shell could not find the command to execute.
Flash Cards
What does exit code 0 mean? — The command succeeded.
What does a non-zero exit code mean? — The command failed; the value indicates the error type.
How do you read the last exit code? — echo $? right after the command runs.
What does 128+N signify? — The process was terminated by signal N (e.g. 137 = 128+9 = SIGKILL).