Logout command

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

The logout command is used to exit a login shell under Linux and Unix-like operating systems.

Purpose

This command always exits a Bash login shell with optional exit status N. It returns an error if not executed in a login shell.

Syntax

The syntax is:

logout
logout [n]

Examples

Open the terminal window/application. Then type the following command to exit and close the terminal session:

logout

Please note that you can type CTRL+D keyboard shortcuts keys to get the same result. For instance, let us login using ssh command and then log out by pressing the CTRL+d keyboard shortcut combo:

# Step 1. ssh login 
ssh vivek@192.168.2.17

# Step 2. Do some work
uptime
w
top

# Step 3. Log out by pressing ctrl+d or type 'logout'
logout
Logout command demo
Logout command demo

A note about subshell

Sometimes we log into a subshell, and we can exit from it too:

# Step 1. Show the current Subshell Level with $SHLVL
echo "$SHLVL"

# Step 2. Start a new session
bash

# Step 3. See the current Subshell Level with $SHLVL
echo "$SHLVL"

# Step 4. How do I get out subshell? Use logout?
logout

# ***************WARNING*********************************
# No, the logout command won't work with my subshell. 
# You will see a message  as follows:
# "bash: logout: not login shell use `exit'"
#
# Hence, we need to use the 'exit' command (or press CTRL+D) 
# to exit the subshell:
# ********************************************************
exit

# Step 5. Verify it
echo "$SHLVL"
To exit the shell and subshell we need to use the exit command
To exit the shell and subshell we need to use the exit command

See also