logout

file managementLinux/Unix
The logout command is one of the most frequently used commands in Linux/Unix-like operating systems. logout Exit a login shell

Quick Reference

Command Name:

logout

Category:

file management

Platform:

Linux/Unix

Basic Usage:

logout [options] [arguments]

Common Use Cases

    Syntax

    logout

    Options

    Option Description
    None The logout command does not accept any options

    Examples

    How to Use These Examples

    The examples below show common ways to use the logout command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    logout
    Exit the current login shell.
    # Advanced Examples Advanced
    if [ "$SHLVL" -gt 1 ]; then logout; fi
    Only logout if in a nested shell.
    echo "Logging out in 5 seconds..."; sleep 5; logout Delay logout with a countdown. logout 2>/dev/null || exit Try to logout, fall back to exit if not in a login shell. [[ $- == *i* ]] && logout || echo "Not an interactive login shell" Check if in an interactive shell before attempting logout.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'logout' command is a shell built-in command used to exit a login shell. It specifically terminates a shell session that was created by the 'login' process or an equivalent that reads the /etc/profile file. This command is primarily used in interactive shell sessions to properly close a user's login session. Key features of the logout command: 1. Login Shell Specificity: logout only works in login shells, not in subshells or non-login shells. In non-login shells, it will produce an error message like "Not login shell" or "not login shell: use 'exit'". 2. Clean Session Termination: When executed in a login shell, logout properly terminates the session, ensuring that all appropriate logout procedures are followed, including updating login records and running logout scripts. 3. Shell Integration: As a built-in command, logout's behavior is specific to the shell in which it's run (bash, zsh, etc.). It is not a standalone executable program like many other commands. 4. No Options: The logout command is one of the simplest commands, accepting no options or arguments, as its purpose is straightforward. 5. Login Records: When used to exit a login shell, logout updates the system's login records to show that the user has terminated their session. 6. Cleanup Operations: logout runs any necessary cleanup operations defined by the shell or system for terminating a login session, such as executing the ~/.bash_logout script in bash. 7. Terminal Management: In terminal environments, logout will close the terminal window or return control to the login prompt, depending on the system configuration. Common use cases for the logout command include: - Ending an interactive terminal session - Properly terminating a user session when work is complete - Running logout scripts to perform cleanup operations - Securing a terminal by ensuring the user is completely logged out - Scripting session management in environments where login shells are used It's important to understand the distinction between 'logout' and 'exit'. While both can terminate a shell session, 'logout' is specifically for login shells and will fail in non-login shells, whereas 'exit' works in any shell. In practice, 'exit' is more commonly used as it works in all shell contexts. For users and administrators, the main value of using logout rather than exit is that it ensures proper session cleanup and login record updates when terminating a login session, which can be important for system accounting and security purposes.

    Related Commands

    These commands are frequently used alongside logout or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the logout command works in different scenarios.

    $ logout
    View All Commands