whatis exit-code
Exit Code
Also known as: exit status, return code, $?
The number from 0 to 255 a program returns when it finishes. Zero means success; anything else signals an error.
What Is an Exit Code in Linux?
The shell stores the last command's exit status in $?. Constructs like if, &&, and || all test it, so mkdir dir && cd dir only changes directory if mkdir succeeded.
Some codes are conventional: 1 is a general error, 2 is misuse, 126 is "not executable", 127 is "command not found", and 128 plus N means the process was killed by signal N (so 137 is SIGKILL, often from the OOM killer).
Example
grep -q root /etc/passwd; echo $?
# 0 Learn more about Exit Code
- Exit Codes Explained: What $? Is Telling You Every command returns a number. Zero means success and everything else means something specific, including the codes that encode which signal killed a process.
- Bash Error Handling: set -euo pipefail and Why Your Script Needs It By default a bash script that fails halfway through keeps running as if nothing happened. Three options change that, and knowing exactly what each one does is the difference between safety and mystery.
- trap and Signal Handling in Shell Scripts Cleaning up temporary files on exit, handling Ctrl+C properly, and why EXIT is the only trap most scripts need.