exit
100%

Command Line · Lesson 19

exit

Learn how to leave the current shell and choose the status it returns to its caller.

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.

You started Bash inside another shell and now want to return to the parent shell. Which command should you run in the nested Bash session?

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.

Which command terminates the current shell and explicitly reports success to its caller?

In Bash, what status does exit return when you do not provide a number?

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.

Which Bash builtin is specifically intended to leave a login shell?

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.

Lesson complete

You finished exit

You can now leave the current shell and communicate its completion status.

  • Use exit to return to the current shell's caller.

  • Supply 0 for success or a defined nonzero status otherwise.

  • Understand the status used by a bare exit.

  • Use logout only for a login shell.

  • Recognize Ctrl+D as end-of-file input rather than a signal.

Keep your learning progress

Create a free account to save this lesson and continue learning on any device.

Create a free account
Back to Command Line