The difference between the login shell and the Non-login shell

Source: Internet
Author: User

Bash should be the most important thing we have in daily work, like our most loyal friends, and we need to know the "habits" of this friend.
Bash has several different modes of operation, the login shell and the Non-login shell,interactive shell and the non-interactive shell (such as executing shell scripts). The two classifications are interleaved, meaning that a login shell might be a interactive shell or a non-interactive shell.
In the following cases, we can obtain a login shell:

    1. The top-level shell you get when you log in to your system, whether you're logged in via a local terminal or via a network ssh. The login shell obtained in this case is an interactive shell.
    2. You can get an interactive login shell by calling bash with the--login option under terminal.
    3. Use the--login option in the script to invoke bash (for example, in the first line of the shell script as specified below: #!/bin/bash--login), you get a non-interactive login shell.
    4. When you use "Su-" to switch to the specified user, you get the login shell for this user. If you do not use "-", you get the non-login shell.

The main difference between the login shell and the Non-login shell is that they read different configuration files when they start, causing the environment to differ. Login Shell starts by reading the/etc/profile global configuration, then finds the ~/.bash_profile, ~/.bash_login, ~/.profile three configuration files, and reads the first found and readable file. The login shell reads and executes the commands in ~/.bash_logout when it exits.
The interactive Non-login shell reads the ~/.BASHRC resource file when it starts. The non-interactive non-login shell does not read all of the above configuration files, but instead looks for the environment variable bash_env, reading and executing the commands in the file that bash_env points to.
If you call Bash,bash with the command "sh" it will be as backward compatible as possible. When started as the login shell, bash reads the/etc/profile and ~/.profile configuration files sequentially. As the Non-login shell starts, bash reads the environment variable env points to the file.
Usually we want to customize some configurations, write the configuration in ~/.BASHRC, and then read the ~/.BASHRC in ~/.bash_profile, which ensures that the login shell and the interactive non-login shell get the same configuration. As for/etc/profile do not easily change, after all, will affect the overall system configuration.
The following is a simple experiment to verify the above description. Set the following variables in ~/.bash_profile:

Lshell= "Login Shell would see this message"

Launch an interactive non-login shell and interactive login shell, respectively, to view the Lshell variables:

[[Email protected] ~]$ bash[[Email protected] ~]$ Echo $lshell [email protected] ~]$ Exitexit[[email protected " ~]$ bash--login[[email protected] ~]$ Echo $lshelllogin Shell would see this message[email protected] ~]$ Exitlogout                

The difference between the login shell and the Non-login shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.