exit
Terminate a shell or script
TLDR
Exit with the exit status of the most recently executed command
Exit with a specific exit status
SYNOPSIS
exit [n]
PARAMETERS
n
Optional integer (0-255) specifying the exit status. Defaults to last command's status.
DESCRIPTION
The exit command is a core shell builtin in Unix-like systems, designed to terminate the current shell process or script execution. Invoked without arguments, it causes the shell to exit using the status code of the most recent command (0 typically indicates success, non-zero failure). Providing an integer argument n sets the explicit exit status, which is returned to the parent process.
In interactive shells, exit (or Ctrl+D) logs out the user from login shells, running logout procedures like clearing history or executing /etc/logout scripts. In non-interactive scripts, it halts execution immediately, making it essential for error handling, such as if ! command; then exit 1; fi.
As a builtin (not an external binary), exit is available in all POSIX-compliant shells like Bash, Zsh, Dash, and Ksh. It supports exit codes 0-255; higher values may be truncated. In functions or sourced files (. file), exit propagates to the parent shell, unlike return which only exits the function. POSIX mandates its behavior for portability.
Common in automation, CI/CD pipelines, and daemons to signal completion or errors cleanly.
CAVEATS
No options/flags supported (builtin only). Exits parent shell if used in sourced scripts. In traps, terminates shell immediately. Non-zero codes above 255 may wrap around.
EXAMPLES
exit 0
# Exit successfully
exit 1
# Exit with error
if ! grep pattern file; then
exit 127
fi
INTERACTIVE USE
Type exit or press Ctrl+D to logout from terminal.
HISTORY
Originated in Thompson Shell (Version 7 Unix, 1979). Evolved through Bourne Shell (1977 standards). Standardized in POSIX.1-1988 for portability across shells.


