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
An optional integer representing the exit status to be returned to the parent process. This value should be between 0 and 255. If omitted, the exit status of the last executed command is used.
DESCRIPTION
The exit command is a built-in shell command used to terminate the current shell execution environment, whether it's an interactive shell session, a shell script, or a shell function. When exit is executed, the shell process terminates, and control is returned to its parent process. An exit status (also known as a return code or exit code) can be provided as an argument, which is then passed back to the parent process. This status is a numerical value typically used to indicate the success or failure of the exited process.
By convention, an exit status of 0 (zero) signifies success, while any non-zero value typically indicates a failure or an error condition. Different non-zero values can be used to distinguish between various types of errors. If no argument is supplied to exit, the exit status defaults to the exit status of the last command executed before exit. The exit status is an integer ranging from 0 to 255. If a value outside this range is provided, it is usually taken modulo 256. This command is crucial for controlling flow in shell scripts, allowing scripts to signal their outcome to calling programs or other scripts.
CAVEATS
The exit status is an integer in the range 0-255. Values outside this range will be truncated (modulo 256).
When exit is used inside a subshell or a shell function, it will only terminate that specific subshell or function, not necessarily the parent script, unless the subshell is the main execution context.
If exit is called without an argument, its status is that of the last command executed. Be mindful of this default behavior when debugging scripts.
Using exit in an interactive login shell will terminate your shell session.
EXIT STATUS CONVENTIONS
The convention of 0 for success and non-zero for failure is universally adopted in Unix-like systems. This allows scripts and programs to reliably check the outcome of executed commands. The exit status of the most recently executed foreground command is stored in the special shell parameter $?, which can be accessed for conditional logic within scripts.
BUILT-IN NATURE
Unlike external programs which are located in the file system (e.g., /bin/ls), exit is a built-in command. This means it is an integral part of the shell's executable code, making its execution faster as the shell does not need to search for it in the PATH environment variable or create a new process for it.
HISTORY
The exit command has been a fundamental built-in feature of Unix shells since their early days, including the original Bourne shell (sh) and subsequent shells like csh, ksh, and bash. Its behavior, particularly regarding exit status handling, is standardized by POSIX (Portable Operating System Interface), ensuring consistent operation across different compliant shells. It is a cornerstone for shell scripting, enabling explicit control over program termination and inter-process communication via exit codes.