The configuration file loading process at bash startup,

Source: Internet
Author: User

The configuration file loading process at bash startup,

Directory:

1.1 determine whether it is interactive or logon

1.2 common bash startup Methods

1.3 load the bash environment configuration file

When a user logs on to the system, various bash configuration files will be loaded, a series of variables will be set or cleared, and sometimes some custom commands will be executed. These actions are the process of starting bash.

In addition, sometimes login to the system can interact (such as normal login to the system), sometimes there is no interaction (such as executing a script ), therefore, bash startup types can be divided into interactive shell and non-interactive shell. The interactive shell can also be divided into interactive Login shell and interactive non-login shell, in some cases, the non-interactive shell can be followed by the bash command with the "-- login" or short option "-l". In this case, the non-interactive login shell is considered as a logon shell.

1.1 determine whether it is interactive or logon

There are two simple methods to determine whether an interactive shell is used:

Method 1: Determine the variable "-". If the value contains the letter "I", it indicates the interaction.

[root@xuexi ~]# echo $-himBH[root@xuexi ~]# vim a.sh#!/bin/bashecho $-[root@xuexi ~]# bash a.shhB

Method 2: Judge the variable PS1. If the value is not empty, the variable is interactive. Otherwise, the variable is non-interactive because the non-interactive variable is cleared.

[root@xuexi ~]# echo $PS1[\u@\h \W]\$

It is easy to determine whether the log-on type is used. You only need to execute "shopt login. If the value is "on", the logon mode is used. Otherwise, the logon mode is not used.

[root@xuexi ~]# shopt login_shell  login_shell     on
[root@xuexi ~]# bash[root@xuexi ~]# shopt login_shelllogin_shell     off

Therefore, you can use the following command to determine whether it is interactive or logon:

Echo $ PS1; shopt login_shell
Or
Echo $-; shopt login_shell

1.2 common bash startup Methods

(1) normal Logon (Pseudo Terminal logon, such as ssh logon or virtual terminal logon) is an interactive logon shell.

[root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$login_shell     on

(2). the su command, without "-- login", is an interactive, non-Logon shell with "-- login", and is a logon shell.

[root@xuexi ~]# su root[root@xuexi ~]# echo $PS1;shopt login_shell [\u@\h \W]\$login_shell     off
[root@xuexi ~]# su -Last login: Sat Aug 19 13:24:11 CST 2017 on pts/0[root@xuexi ~]# echo $PS1;shopt login_shell[\u@\h \W]\$login_shell     on

(3). Execute the bash command without the "-- login" option as an interactive, non-Logon shell. But when "-- login" is specified, it is an interactive and logon shell.

[root@xuexi ~]# bash[root@xuexi ~]# echo $PS1;shopt login_shell[\u@\h \W]\$login_shell     off
[root@xuexi ~]# bash -l[root@xuexi ~]# echo $PS1;shopt login_shell[\u@\h \W]\$login_shell     on

(4). When you use a combination of commands (surrounded by parentheses) and replace the command with a sub-shell, the interaction and login attributes of the parent shell are inherited.

[root@xuexi ~]# (echo $BASH_SUBSHELL;echo $PS1;shopt login_shell)1[\u@\h \W]\$login_shell     on
[root@xuexi ~]# su[root@xuexi ~]# (echo $BASH_SUBSHELL;echo $PS1;shopt login_shell)1[\u@\h \W]\$login_shell     off

(5). When the remote command is executed through ssh but is not logged on, it is non-interactive or non-logon.

[root@xuexi ~]# ssh localhost 'echo $PS1;shopt login_shell' login_shell     off

(6) The shell script is non-interactive or non-Logon shell. But when "-- login" is specified, it is a non-interactive, login shell.

For example, the script content is as follows:

[root@xuexi ~]# vim b.sh#!/bin/bashecho $PS1shopt login_shell

If the "-- login" option is not included, it is a non-interactive, non-Logon shell.

[root@xuexi ~]# bash b.sh login_shell     off

The "-- login" option is not an interactive or logon shell.

[root@xuexi ~]# bash -l b.sh login_shell     on

(7). When a terminal is opened on the GUI, it is an interactive, non-Logon shell.

 

However, you can use interactive and logon shell.

 

1.3 load the bash environment configuration file

Bash always configures its runtime environment regardless of interaction or logon. Bash environment configuration is mainly completed by loading the bash environment configuration file. However, whether or not to interact or whether to log on will affect the configuration files to be loaded. In addition to the interaction and login attributes, some special attributes will also affect the way to read the configuration file.

Bash environment configuration files include/etc/profile ,~ /. Bash_profile ,~ /. Bashrc,/etc/bashrc, and/etc/profile. d /*. sh: To test which configuration files are read in various situations, write several echo statements to these configuration files to determine whether the configuration file is read and loaded when bash is started.

echo "echo '/etc/profile goes'" >>/etc/_profileecho "echo '~/.bash_profile goes'" >>~/.bash_profileecho "echo '~/.bashrc goes'" >>~/.bashrcecho "echo '/etc/bashrc goes'" >>/etc/bashrcecho "echo '/etc/profile.d/test.sh goes'" >>/etc/profile.d/test.shchmod +x /etc/profile.d/test.sh

①. Interactive login shell or non-interactive login with "-- login" (or short option "-l", for example, specifying "#! /Bin/bash-l ") When bash is started, it reads/etc/profile first, and then searches ~ /. Bash_profile ,~ /. Bash_login and ~ /. Profile, and only load the first searched and readable file. When you exit ~ /. Bash_logout.

Note that the/etc/profile is loaded in/etc/profile. d /*. sh statement, it will use source to load/etc/profile. d/All executable scripts with the sh suffix.

[root@xuexi ~]# grep -A 8 \*\.sh /etc/profile  for i in /etc/profile.d/*.sh ; do    if [ -r "$i" ]; then        if [ "${-#*i}" != "$-" ]; then            . "$i"        else            . "$i" >/dev/null 2>&1        fi    fidone

["$ {-# * I}" in the inner if statement }"! = "$-" Indicates that "$-" matches "* I" from left to right and deletes the matched content (that is, segmentation of variables ), if the value after "$-" splitting is not equal to "$-", it means an interactive shell, so what is it, or what is it.

Similarly, in ~ /. Bash_profile is also loaded ~ /. Bashrc command.

[root@xuexi ~]# grep -A 1 \~/\.bashrc ~/.bash_profileif [ -f ~/.bashrc ]; then        . ~/.bashrcfi

And ~ /. Bashrc contains the command for loading/etc/bashrc.

[root@xuexi ~]# grep -A 1 /etc/bashrc ~/.bashrcif [ -f /etc/bashrc ]; then        . /etc/bashrcfi

In fact, the/etc/profile. d/*. sh statement is still available in/etc/bashrc, but it is executed only when the non-Logon shell is used. Some statements are as follows:

if ! shopt -q login_shell ; then   # We're not a login shell...    for i in /etc/profile.d/*.sh; do        if [ -r "$i" ]; then            if [ "$PS1" ]; then                . "$i"            else                . "$i" >/dev/null 2>&1            fi        fi    done...fi

The internal if statement and the corresponding judgment statement in/etc/profile have the same effect, except that the judgment method is different and the writing method is different.

Therefore, the actual process of loading the bash environment configuration file through the interactive login shell is as follows:

 

The following results validate the conclusion:

Last login: Mon Aug 14 04:49:29 2017 #/etc/profile. d/*. sh goes/etc/profile goes/etc/bashrc goes ~ /. Bashrc goes ~ /. Bash_profile goes
[Root @ xuexi ~] # Ssh localhost # root @ localhost's password: Last login: Mon Aug 14 05:05:50 2017 from 172.16.10.1/etc/profile during ssh remote logon. d /*. sh goes/etc/profile goes/etc/bashrc goes ~ /. Bashrc goes ~ /. Bash_profile goes
[Root @ xuexi ~] # Bash-l # Run/etc/profile. d/*. sh goes/etc/profile goes/etc/bashrc goes ~ /. Bashrc goes ~ /. Bash_profile goes
[Root @ xuexi ~] # Su-# When su carries "-- login",/etc/profile. d/*. sh goes/etc/profile goes/etc/bashrc goes ~ /. Bashrc goes ~ /. Bash_profile goes
[Root @ xuexi ~] # Vim a. sh # When executing a shell script with "-- login #! /Bin/bash-lecho haha [root @ xuexi ~] #./A. sh/etc/profile goes/etc/bashrc goes ~ /. Bashrc goes ~ /. Bash_profile goeshaha

The reason for executing the shell script is that the execution of/etc/profile is not displayed. d /*. sh, because it is non-interactive, according to [if ["$ {-# * I}" in/etc/profile }"! = "$-"], Which redirects the execution result of/etc/profile. d/*. sh to/dev/null. That is to say, even a shell script (with the "-- login" option) loads all the bash environment configuration files.

②. When the interactive non-Logon shell bash is started, it will read ~ /. Bashrc, does not read/etc/profile and ~ /. Bash_profile ,~ /. Bash_login and ~ /. Profile.

Therefore, the actual process of loading the bash environment configuration file using an interactive non-login shell is shown in the box:

For example, when executing a bash or su command without "-- login.

[root@xuexi ~]# bash/etc/profile.d/*.sh goes/etc/bashrc goes~/.bashrc goes
[root@xuexi ~]# su/etc/profile.d/*.sh goes/etc/bashrc goes~/.bashrc goes

③. When a non-interactive, non-Login shell starts bash, it does not load any aforementioned bash environment configuration file, but searches for the variable BASH_ENV. If it is found, it loads the specified file. But not all non-interactive, non-Login shell startup will be like this, see situation 4.

It is like the following statement:

if [ -n "$BASH_ENV" ];then    . "$BASH_ENV"fi

Almost all shell scripts are executed without the "-- login" option. Therefore, shell scripts do not load any bash environment configuration files unless the variable BASH_ENV is manually configured.

④. Bash started in remote shell mode. Although it is non-interactive or non-Logon, it loads ~ /. Bashrc, so it will load/etc/bashrc. Because it is not a logon type, it will eventually load/etc/profile. d /*. sh, but because it is not interactive, all the execution results are redirected to/dev/null.

If you understand rsync, you will know that it has a remote shell connection method. The remote shell method starts bash through the network and associates the bash standard output, just as it connects to a remote shell daemon. Generally, sshd is used to achieve this connection. The old version of rshd also supports this connection.

This is also true. When you use ssh to connect to a remote host but do not log on to the remote host (for example, to execute a remote command), it is a remote shell method, however, it is a non-interactive, non-login shell.

[root@xuexi ~]# ssh localhost echo haharoot@localhost's password:/etc/bashrc goes~/.bashrc goeshaha

As mentioned above, it also loads/etc/profile. d /*. sh, except that the if judgment statement [if ["$ PS1"]; then] in/etc/bashrc causes the non-interactive shell to redirect the execution result to/dev/null.

 

Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprinted please indicate the source: Success!

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.