Shell related ------- each environment variable configuration file and the corresponding shell mode

■Each configuration file

1./etc/profile

This script must be executed when each user in the system logs in. If the system administrator wants a certain setting to take effect for all users, it can be written in this file

 

2.~/.bash_profile、~/.bash_login和~/.profile

These are defined in the current user's home directory, in fact, mainly ~/.bash_profile, the other two generally do not seem to exist.

[root@localhost sabopeusr0]# ls -a ~
.  ..  .bash_history  .bash_logout  .bash_profile  .bashrc  .cshrc  .tcshrc  anaconda-ks.cfg

 

~/.bash_profile, if you want a setting to take effect only for the current user, you can write it in this file.

Since this script is executed after /etc/profile, the values ​​of some environment variables set by /etc/profile can be modified in this script,
that is, the settings of the current user can override the global settings in the system.

The startup script ~/.profile is specified by sh, and bash specifies that the startup file starting with ~/.bash_ should be searched first.

If not, execute ~/.profile, in order to be consistent with sh.

 

3.~/.bash_logout

The ~/.bash_logout script (if it exists) is executed on logout.

 

4.~/.bashrc

Open a terminal window under the graphical interface, or enter the bash command at the login shell prompt to get an interactive non-login shell.
This kind of shell automatically executes the ~/.bashrc file at startup and executes it as a fork subshell, that is, inherits the environment variables of the parent shell and modifies it according to .bashrc.
After the execution, the local variables will not be saved to the parent shell. .

[root @ localhost sabopeusr0] # ps -ef | grep -v grep | grep bash
sabopeu + 8906 8905 0 7 月 26 pts / 2 00:00:00 -bash
root 11223 11222 0 09:41 pts / 2 00:00:00 bash
[root @ localhost sabopeusr0] # bash
[root @ localhost sabopeusr0] # ps -ef | grep -v grep | grep bash
sabopeu + 8906 8905 0 7 月 26 pts / 2 00:00:00 -bash
root 11223 11222 0 09: 41 pts / 2 00:00:00 bash
root 11392 11223 0 13:00 pts / 2 00:00:00 bash
[root @ localhost sabopeusr0] #

 

Sometimes in order to make the login shell also automatically execute ~/.bashrc, usually call ~/.bashrc in ~/.bash_profile:
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi
The above means, if The ~/.bashrc file exists then source it. Most Linux distributions automatically create ~/.bash_profile and ~/.bashrc scripts when creating an account
. These lines are usually present in ~/.bash_profile. So, if you want to make some settings in the startup script to make it work in both the graphical terminal window and the shell of the character terminal,
it is best to set it in ~/.bashrc.

 

 

■Shell mode

1. Start an interactive login shell

/etc/profile, ~/.bash_profile, ~/.bash_login and ~/.profile

 

1) Start the login shell in the way of Bash (the shell defined in /etc/passwd is /bin/bash, which is the default way)

/etc/profile⇒~/.bash_profile⇒~/.profile

(The startup script ~/.profile is specified by sh, and bash specifies that the startup file starting with ~/.bash_ will be searched first. If not, ~/.profile will be executed, in order to be consistent with sh.)

 

2) Start the login shell in sh mode (the shell defined in /etc/passwd is /bin/sh, which is the default way)

If you shstart with a command bash, the behavior bashwill be simulated , and those startup files that start with will not be recognized.sh~/.bash_

So, if started as an interactive login shell, or with the --login parameter, the following files are executed in sequence:

/etc/profile⇒~/.profile

 

2. Interactive non-login shell startup 

~/.bashrc

 

Why should the startup scripts of login shells and non-login shells be distinguished?

The original design was considered in this way. If you log in from a character terminal or remotely, then the login shell is the parent process of all other processes of the user, as well as the parent process of other subshells.

Therefore, if the environment variable is set once in the startup script of the login shell, it can be automatically brought to other non-login shells.

However, there is no way to bring the local variables, functions, aliasand other settings of the shell to the subshell, and it needs to be set every time a non-login shell is started.

Therefore, a non-login shell startup script is required, so generally speaking ~/.bash_profile, environment variables ~/.bashrcare set there, and local variables, functions, aliasetc. are set there.

If your Linux has a graphical system, this cannot be set. Since logging in from the window manager of the graphical interface does not generate a login shell, the environment variables should also be ~/.bashrcset there.

 

 3. Non-interactive startup

The subshell that is forked to execute the script is a non-interactive shell. The script file executed at startup is defined by the environment variable BASH_ENV, which is equivalent to automatically executing the following commands:
    if [ -n "$BASH_ENV" ]; then
     . "$BASH_ENV";
     fi
If the value of the environment variable BASH_ENV is not an empty string, its value is used as the file name of the startup file, source this script.

 

Fork a non-interactive shell via sh, then

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

 

In general, if launched as a non-interactive shell, no startup scripts are executed. Usually the Shell scripts we write #! /bin/shstart with, and they all belong to this way. 

 

simple test

 

[sabopeusr0-admin@localhost ~]$ grep tshell .bashrc
tshell="non-login shell will see this message"
export tshell
[sabopeusr0-admin@localhost ~]$ grep lshell .bash_profile
lshell="login shell will see this message"
export lshell
[sabopeusr0-admin@localhost ~]$ bash
[sabopeusr0-admin@localhost ~]$ echo $lshell

[sabopeusr0-admin@localhost ~]$ echo $tshell
non-login shell will see this message
[sabopeusr0-admin@localhost ~]$ exit
exit
[sabopeusr0-admin@localhost ~]$ bash --login
[sabopeusr0-admin@localhost ~]$ echo $lshell
login shell will see this message
[sabopeusr0-admin@localhost ~]$ echo $tshell
non-login shell will see this message
[sabopeusr0-admin@localhost ~]$

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326175207&siteId=291194637