Command Line · Lesson 19 of 19
exit
0 / 4 checkpoints answered
Shells can be nested: a graphical terminal starts a shell, an SSH connection starts a remote shell, and a shell can start another shell. Leaving one normally returns control to whatever started that current shell.
Leaving the Current Shell
The exit command asks the current shell to terminate:
$ exit
If that shell is the main process in a graphical terminal tab, the tab may close according to the terminal's settings. In an SSH session, leaving the remote shell normally returns you to the local shell. If you started a nested shell, exit returns to its parent shell.
CheckYou started Bash inside another shell and now want to return to the parent shell. Which command should you run in the nested Bash session?
press 1/2/3 to answer · → next lesson
Returning an Exit Status
An optional numeric argument sets the status returned to the shell's caller:
$ exit 0
By convention, 0 means success and a nonzero value represents failure or another condition defined by the program. If Bash receives no numeric argument, it exits with the status of the last command executed before exit.
CheckWhich command terminates the current shell and explicitly reports success to its caller?
press 1/2/3 to answer · → next lesson
CheckIn Bash, what status does exit return when you do not provide a number?
press 1/2/3 to answer · → next lesson
Using logout in a Login Shell
The Bash logout builtin exits a login shell:
$ logout
In a non-login Bash shell, logout reports that it is not a login shell; use exit instead.
CheckWhich Bash builtin is specifically intended to leave a login shell?
press 1/2/3 to answer · → next lesson
Using Ctrl+D or Closing a Terminal
At an empty interactive prompt, pressing Ctrl+D normally supplies the terminal's end-of-file input character. Bash commonly interprets that condition as a request to exit. It is not a signal, and shell settings such as Bash's ignoreeof can change the behavior.
Closing a graphical terminal window asks the terminal application to close its processes and can affect running jobs. Prefer an orderly exit when practical, and check for active work before closing a session.
Summary
You can now leave the current shell and communicate its completion status.
- Use
exitto return to the current shell's caller. - Supply
0for success or a defined nonzero status otherwise. - Understand the status used by a bare
exit. - Use
logoutonly for a login shell. - Recognize
Ctrl+Das end-of-file input rather than a signal.
Lesson complete
You finished exit. Nice work — keep the momentum going.