Skip to content

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 n is 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, exit logs you out.
  • In a subshell ( ... ), exit only terminates the subshell.
(
  echo "Inside subshell"
  exit 1
)
echo "Outside subshell (still running)"

Best Practices

  1. Be Specific: Don't just exit 1 for everything. Use specific codes if your caller needs to distinguish errors (e.g., config error vs network error).
  2. Stderr Messages: Always print an error message to stderr (>&2) before exiting with non-zero.
  3. Range Limit: Exit codes are modulo 256. exit 256 becomes 0 (Success!). exit 257 becomes 1. Stick to 0-125 for custom errors.

Notes

  • Ctrl+D (EOF) is equivalent to exit at an empty command prompt.
  • logout command is similar but only works in login shells.