Termination
A shell session terminates in the following cases:
- When the shell reaches the end of input.
- When you use the
exitbuilt-in. - When the shell receives a signal that causes it to terminate, such as
SIGINTorSIGTERM, and no trap is set to handle that signal. - When a non-interactive shell is interrupted by a shell error.
- When a command fails and the
errexitshell option is enabled. (See Exiting on errors.)
Preventing accidental exits
Ignoring EOF
When the input to the shell is a terminal, you can signal an end-of-file with the eof sequence (usually Ctrl+D). However, you might not want the shell to exit immediately when this happens, especially if you often hit the sequence by mistake. Enable the ignoreeof shell option to prevent the shell from exiting on end-of-file and let it wait for more input.
$ set -o ignoreeof
$
# Type `exit` to leave the shell when the ignore-eof option is on.
$ exit
This option is only effective in interactive shells and only when the input is a terminal. As a safeguard against an input that repeatedly delivers EOF, entering 50 eof sequences in a row will still cause the shell to exit.
Suspended jobs
(Since 3.2.0) The shell refuses to exit if there are suspended jobs to prevent them from being killed by the SIGHUP signal sent by the operating system.
$ sleep 100
^Z
[1] + Stopped sleep 100
$
# There are stopped jobs. Type `exit -f` to exit anyway.
$ exit
# There are stopped jobs. Type `exit -f` to exit anyway.
$ exit -f
This applies not only when end-of-file is reached but also when you use the exit built-in. Use exit -f (or exit --force) to bypass the check and exit immediately.
This protection is only effective in interactive shells. When it is triggered by end-of-file, the input must additionally be a terminal, and—as a safeguard against an input that repeatedly delivers EOF—entering 50 eof sequences in a row will still cause the shell to exit. The exit built-in, by contrast, refuses to exit whenever the shell is interactive, regardless of the input type.
Exiting subshells
When one of the above conditions occurs in a subshell, the subshell exits. It does not directly cause the parent shell to exit, but the exit status of the subshell may affect the parent shell’s behavior, conditionally causing it to exit if the errexit option is set.
EXIT trap
You can set a trap for the EXIT condition to run commands when the shell exits. This can be useful for cleanup tasks or logging. The trap is executed regardless of how the shell exits, whether due to an error, end-of-file, or explicit exit command, except when the shell is killed by a signal, in which case the trap is not executed (in yash-rs).
$ trap 'rm -f temporary.txt; echo "Temporary file removed."' EXIT
$ echo "Some data" > temporary.txt
$ cat temporary.txt
Some data
$ exit
Temporary file removed.
The EXIT trap is run at most once per shell session. Modifying the EXIT trap while it is running does not have any effect on trap execution. (However, POSIX allows the shell to execute the new trap if the EXIT trap is redefined while it is running.)
Exit status
If the shell exits due to end of input, the exit built-in, or the errexit option, it returns the exit status of the last command executed. See Exit status of the shell for details.
If the shell exits because of a shell error, the exit status is a non-zero value indicating the error.
Shell errors
The following shell errors set the exit status to a non-zero value and may cause the shell to exit, depending on the situation:
- Unrecoverable errors reading input
- The shell exits immediately.
- This does not apply to scripts read by the
sourcebuilt-in.
- Command syntax errors
- The shell exits if non-interactive.
- If interactive, the shell ignores the current command and resumes reading input.
- Errors in special built-in utilities
- The shell exits if non-interactive or if the
errexitoption is set. Otherwise, it aborts the current command and resumes reading input. - This includes redirection errors for special built-ins.
- This does not apply to special built-ins run via the
commandbuilt-in.
- The shell exits if non-interactive or if the
- Variable assignment errors and expansion errors
- The shell exits if non-interactive or if
errexitis set. Otherwise, it aborts the current command and resumes reading input.
- The shell exits if non-interactive or if
- Redirection errors (except for special built-ins)
- The shell exits if
errexitis set. Otherwise, it continues with the next command.
- The shell exits if
POSIX.1-2024 allows shells to exit on command search errors, but many shells, including yash-rs, do not.