exit Command Cheat Sheet
exit terminates a shell session or a script. It can return a status code (0-255) to the parent process, which is the primary mechanism for communicating success or failure in Unix/Linux.
Synopsis
exit [n]
n: Exit status (integer 0-255).- If
nis omitted, the exit status is that of the last executed command.
Basic Usage
Exit Terminal Session
Closes the current terminal window or logs out of SSH.
exit
Exit Script with Success
exit 0
Exit Script with Error
echo "Error: File missing" >&2
exit 1
Exit Status Codes
The exit code is stored in the special variable $?.
| Code | Meaning | Example Cause |
|---|---|---|
0 |
Success | Normal termination |
1 |
General error | Divide by zero, permissions, etc. |
2 |
Misuse of shell builtin | Syntax error in builtin command |
126 |
Command invoked cannot execute | Permission denied (found but not executable) |
127 |
Command not found | Typo in command name, not in $PATH |
128 |
Invalid argument to exit | exit 3.14 (non-integer) |
130 |
Script terminated by Ctrl+C | Value is 128 + 2 (SIGINT) |
137 |
Script killed by SIGKILL | Value is 128 + 9 (SIGKILL) - e.g. OOM Killer |
255 |
Exit status out of range | exit -1 maps to 255 |
Handling Exit Codes in Scripts
Checking Previous Command
Always check $? immediately after the command run.
cp source.txt dest.txt
if [ $? -eq 0 ]; then
echo "Copy succeeded"
else
echo "Copy failed"
exit 1
fi
Short-Circuit Evaluation
More concise syntax for simple checks.
# Exit if mkdir fails
mkdir /backup || exit 1
# Proceed only if cd succeeds
cd /var/www || { echo "Dir not found"; exit 1; }
Advanced Script Control
Fail Fast (set -e)
Instruct the shell to exit immediately if any command returns a non-zero status.
set -e
cp file1 file2 # If this fails, script dies here
rm file1
Functions and Return
Note that exit terminates the entire script, not just the function. To leave a function but keep the script running, use return.
my_function() {
if [ -z "$1" ]; then
return 1 # Returns status 1, but script continues
fi
exit 1 # Terminates script completely
}
Traps (Cleanup on Exit)
Using exit abruptly might leave temporary files. Use trap to ensure cleanup runs regardless of how the script exits.
temp_file=$(mktemp)
# Run this command on EXIT (code 0) or signal
trap "rm -f $temp_file; echo 'Cleaned up'" EXIT
echo "Working..."
# exit 1 <-- Even if we exit here, cleanup runs
Shell Behavior
Interactive Shells
- In a login shell,
exitlogs you out. - In a subshell
( ... ),exitonly terminates the subshell.
(
echo "Inside subshell"
exit 1
)
echo "Outside subshell (still running)"
Best Practices
- Be Specific: Don't just
exit 1for everything. Use specific codes if your caller needs to distinguish errors (e.g., config error vs network error). - Stderr Messages: Always print an error message to stderr (
>&2) before exiting with non-zero. - Range Limit: Exit codes are modulo 256.
exit 256becomes0(Success!).exit 257becomes1. Stick to 0-125 for custom errors.
Notes
Ctrl+D(EOF) is equivalent toexitat an empty command prompt.logoutcommand is similar but only works in login shells.