LinuxCommandLibrary

logout

Terminate the current login session

TLDR

Exit a login shell

$ logout
copy

Exit a login shell and specify a return value
$ logout [exit_code]
copy

SYNOPSIS

logout

DESCRIPTION

The logout command is a shell built-in, meaning its functionality is provided directly by the command interpreter (e.g., bash, csh, tcsh) rather than being a separate executable program.

It is primarily used to terminate the current login shell session, effectively ending the user's interactive session. While similar to the exit command, which terminates any shell instance, logout is specifically designed for login shells. This implies that it typically only works if the current shell is the initial shell started after a user's authentication (e.g., via ssh, direct console login, or graphical terminal emulator configured to launch a login shell).

Upon successful execution, logout usually results in the closing of the terminal window or the disconnection of the SSH session from which the command was issued. This command is particularly relevant in command-line environments, such as when working on remote servers via SSH, or within a standalone terminal session on a local machine. It ensures a clean departure from the system, releasing resources and closing open connections related to the session.

CAVEATS

Shell Built-in Specificity: logout is a shell built-in command, meaning its behavior can slightly vary between different shells (e.g., bash, csh, tcsh). It is not a standalone executable found in standard system paths.

Login Shell Requirement: It generally only works if the current shell is the initial login shell. If executed in a subshell, it might behave like exit or simply fail.

No Arguments/Options: The command typically accepts no arguments or options.

Running Jobs: Some shells may prevent logout if there are background jobs running without nohup, prompting the user or requiring explicit termination of jobs before logging out.

Comparison with exit: For non-login shells or general script termination, exit is often a more universal and reliable choice.

DIFFERENCES WITH EXIT

While both logout and exit serve to terminate a shell, their primary distinctions lie in their scope and typical usage. logout is specifically intended for login shells; it usually only works if the current shell is the initial shell opened at login and often triggers a complete session termination (e.g., closing the terminal window or disconnecting an SSH session). Conversely, exit can terminate any shell (login or non-login) and is more commonly used in scripts, functions, or to simply close a subshell without necessarily ending the entire user session.

HISTORY

The logout command has been a fundamental feature of Unix-like shells, particularly the C shell (csh) and its derivatives like tcsh, for many decades. Its inclusion provided a distinct command for terminating a user's interactive session, differentiating it from the more general exit command which could terminate any shell instance.

While still present and functional in modern shells like bash, its usage might be less prominent in graphical desktop environments where users often rely on GUI-driven logout options. However, for server administration, remote access via SSH, and traditional command-line work, logout remains a simple and direct method for session termination.

SEE ALSO

exit(1), login(1), ssh(1), su(1)

Copied to clipboard