December 1, 2026
Bash terminal showing exit code examples

Bash exit codes tell you whether a command succeeded or failed and why. This guide covers the most common exit codes – 0, 1, 127, and 137 – what they mean, and how to use them in scripts.

Exit code basics

Every command in Bash returns an exit code when it finishes. A value of 0 means success. Any non-zero value means failure. The special variable $? holds the exit code of the last command.

“`bash
ls /etc/passwd
echo $?
“`

Output:

“`
0
“`

“`bash
ls /nonexistent
echo $?
“`

Output:

“`
2
“`

Common exit codes

Code Meaning Example cause
0 Success Command completed without errors
1 General error Command failed for an unspecified reason
127 Command not found The command does not exist or is not in PATH
128 Invalid exit argument Exit called with an invalid value
130 Terminated by Ctrl+C Process received SIGINT
137 Killed by OOM killer Process received SIGKILL (exit code 128 + 9)
139 Segmentation fault Process received SIGSEGV
255 Exit status out of range Return value outside 0-255

Exit code 127: Command not found

Exit code 127 means Bash could not find the command. Common causes:

  • The command is not installed
  • The command is not in PATH
  • A typo in the command name
  • A script file lacks execute permission

Example:

“`bash
mycommand
echo $?
“`

Output:

“`
127
“`

Fix by installing the package, adding the directory to PATH, correcting the spelling, or running chmod +x script.sh.

Exit code 137: Killed by OOM killer

Exit code 137 means the process received SIGKILL (signal 9). The formula is 128 + signal number: 128 + 9 = 137. On Linux, this usually means the OOM killer terminated the process because memory was exhausted.

Confirm with kernel logs:

“`bash
dmesg | grep -i killed
“`

Or check the process’s oom_score:

“`bash
cat /proc/self/oom_score
“`

For a full explanation of OOM kills and how to prevent them, see OOM Killer: Why Linux Killed Your Process.

Exit code 1: General error

Exit code 1 is a catch-all for failures that do not fit a more specific code. Many commands return 1 for any failure. To diagnose, run the command directly and read the error message on stderr:

“`bash
some-command 2>&1
“`

Using exit codes in scripts

Check exit codes conditionally:

“`bash
if grep -q “pattern” /etc/config; then
echo “Pattern found”
else
echo “Pattern not found, exit code was $?”
fi
“`

Exit a script with a specific code:

“`bash
exit 1
“`

Chain commands so later commands only run if earlier ones succeed:

“`bash
command1 && command2
“`

Or run a fallback if a command fails:

“`bash
command1 || echo “Command1 failed with exit code $?”
“`

set -e: Exit on any error

Adding set -e at the top of a script makes it exit immediately if any command returns non-zero:

“`bash
#!/bin/bash
set -e

command1
command2
“`

Use set -euo pipefail for stricter error handling:

  • -e: Exit on error
  • -u: Exit on unset variable
  • -o pipefail: Return the exit code of the last failed command in a pipeline

Without pipefail, a pipeline returns the exit code of the last command only, which can hide failures:

“`bash
# Without pipefail: this returns 0 even if grep fails
cat file.log | grep “pattern”

# With pipefail: this returns grep’s exit code if grep fails
set -o pipefail
cat file.log | grep “pattern”
“`

Related reading

For process and memory monitoring commands that help diagnose exit code 137, see Linux Process & Memory Commands Cheat Sheet. For disk space issues that can cause applications to fail, see du and df: Find What’s Filling Your Disk on Linux.

Leave a Reply

Your email address will not be published. Required fields are marked *