exit works in any shell. logout only works in a login shell — elsewhere it refuses with an error. Ctrl+D sends EOF, which normally acts like exit, but can be configured to refuse. Here is each difference, run and shown, not just asserted.
Table of Contents
logout vs exit: one errors, one doesn’t
$ bash -c 'exit 0'; echo $?
0
$ bash -c 'logout' 2>&1; echo $?
bash: line 1: logout: not login shell: use `exit'
1
logout checks whether the shell is a login shell (one started with -l, or your Terminal’s default login shell) before doing anything. A non-login shell — a script, a subshell, a command run as ssh host 'command', a shell launched by another program — refuses outright with exactly that message. exit has no such check; it works everywhere logout does and everywhere it doesn’t.
Ctrl+D normally exits — until ignoreeof is set
Ctrl+D at an empty prompt sends EOF, which bash treats like an unwritten exit. Simulated by closing stdin (the same signal Ctrl+D sends) against an interactive shell:
$ printf '' | bash -i
bash-5.3$ exit # EOF alone ended the session, status 0
$ printf 'set -o ignoreeof\necho before-eof\n' | bash -i
bash-5.3$ set -o ignoreeof
bash-5.3$ echo before-eof
before-eof
bash-5.3$ Use "exit" to leave the shell.
bash-5.3$ Use "exit" to leave the shell.
[... repeats 8 more times ...]
bash-5.3$ exit # bash exits anyway, after IGNOREEOF's default of 10
With set -o ignoreeof, EOF no longer closes the session — bash prints the refusal and keeps the prompt. It isn’t a permanent lock: the IGNOREEOF variable defaults to 10, so 10 consecutive EOFs still force an exit. Ctrl+D is the one of the three that can be made to behave differently depending on shell options; the other two can’t.
Exit status with no argument: it’s the last command’s
$ bash -c 'false; exit'; echo $?
1
$ bash -c 'true; exit'; echo $?
0
$ bash -c 'exit 47'; echo $?
47
exit with no number doesn’t mean success — it passes through whatever the previous command returned. A script that ends on a failing command and calls bare exit reports failure to its caller even if that wasn’t the intent.
Background jobs don’t wait for you, and don’t get killed either
$ cat script.sh
sleep 5 &
echo "started $!"
exit 0
$ time ./script.sh
started 68163
real 0m0.002s # exits immediately, does not wait for the job
$ ps aux | grep "sleep 5"
[user] 68163 ... sleep 5 # still running, now reparented, after the script exited
A non-interactive script’s exit does not wait for background jobs and does not send them any signal — they keep running, orphaned. (Interactive shells behave differently, login shell or not: bash warns “There are stopped jobs.” once before letting a second exit proceed (it warns about running jobs too only if shopt -s checkjobs is set), and closing the terminal hangs up the jobs whatever huponexit is set to, because an interactive shell resends the SIGHUP it receives to its jobs before exiting, while huponexit governs a login shell’s own exit — not tested here, since this was a script, not an interactive session.)
trap ... EXIT: cleanup that always runs
$ bash -c 'trap "echo CLEANUP RAN" EXIT; echo doing work; exit 3'; echo $?
doing work
CLEANUP RAN
3
The trap fires on every exit path — normal completion, an explicit exit N, or an uncaught error — and the script’s own exit status (3 here) is preserved afterward, not overwritten by whatever the trap itself returns. The logout refusal outside a login shell, bare exit passing through the last command’s status and trap ... EXIT leaving an explicit status alone are each checked by how-to-exit-bash.sh, which prints a PASS or FAIL line per claim; its ignoreeof and background-job checks are best-effort and report SKIP when they can’t be observed.
For other shell basics — navigating, checking exit status of individual commands, and more — see this site’s terminal commands cheat sheet.