Exit built-in
The exit built-in causes the currently executing shell to exit.
Synopsis
exit [-f] [exit_status]
Description
exit exit_status makes the shell exit from the currently executing
environment with the specified exit status.
The shell executes the EXIT trap, if any, before exiting, except when the built-in is invoked in the trap itself.
(Since 3.2.0) In an interactive shell, if there are
suspended jobs, the built-in prints a warning
and refuses to exit. Use the -f option to override this protection and exit
immediately.
Options
-f,--force- (Since 3.2.0) Force exit even when there are suspended jobs. Without this option, the built-in prints a warning and returns a non-zero exit status if there are any suspended jobs in an interactive shell.
Operands
The optional exit_status operand, if given, should be a non-negative decimal integer and will be the exit status of the exiting shell process.
Errors
If the exit_status operand is given but not a valid non-negative integer, it is a syntax error.
This implementation treats an exit_status value greater than 2147483647 as a syntax error.
If there are suspended jobs in an interactive shell and -f is not given,
the built-in prints a warning to standard error and returns exit status 1
without exiting.
Exit status
The exit_status operand specifies the exit status of the exiting shell.
If the operand is not given, the shell exits with the current exit status
($?). If the built-in is invoked in a trap, the exit status will be the
value of $? before entering the trap.
In case of an error, the exit status is 2.
If the exit status indicates a signal that caused the process of the last command to terminate, the shell terminates with the same signal. See Exit status of the shell for details.
Examples
To exit the shell with exit status 42:
$ exit 42
To force exit when there are suspended jobs:
$ sleep 100
^Z
[1] + Stopped sleep 100
$ exit
# There are stopped jobs. Type `exit -f` to exit anyway.
$ exit -f
Compatibility
The exit built-in is specified by POSIX.1-2024.
In some shells, the exit built-in lacks support for the -- separator.
The behavior is undefined in POSIX if exit_status is greater than 255. The current implementation passes such a value as is in the result, but this behavior may change in the future.
The -f/--force option and the suspended-jobs protection are
non-portable extensions.