[linux] Linux environment variable initialization and the order in which corresponding files take effect

1. Know the environment variable (PATH)

The environment variable (PATH) is a list, which contains the addresses of some programs in order (usually xxx/bin). After adding the environment variable, the system can recognize the command and execute the program corresponding to the command when entering a command in any directory. The function of environment variables is to tell the system where to look.

The order of system search is: current directory - system variable list in order - environment variable list in order

2. Know the environment file

(1) /etc/profile: System configuration file, read once when the user logs in
(2) /etc/bash.bashrc: (Ubuntu) system configuration file, read once when the user logs in, each time a new terminal session is opened Read it once.
/etc/bashrc: (Centos) system configuration file, read once when the user logs in and once each time a new terminal session is opened.
(3) ~/.profile (~/.bash_profile, ~/.bash_login): User profile, read once when the user logs in
(4) ~/.bashrc: User profile, read once when the user logs in, each time Read once when opening a new terminal session

1. The system configuration file acts globally, while the user configuration file is only for the currently logged in user.
2. The system configuration file is read first, and then the user configuration file is read. The variables and expressions of the user configuration file are inherited from the system configuration file.

3. Environment variable initialization principle

When logging into a Linux system and starting a bash shell, by default bash will search for environment variable settings in several files. These files can be collectively referred to as system environment files. The environment variable files checked by bash depend on the system running the shell. There are three ways for the system to run Shell:

(1) Shell that runs by default after logging in as a system user
(2) Non-login interactive shell
(3) Execute script to run non-interactive Shell

These three types of effects on environment variables can be divided into two categories: login shell and non-login shell.

3.1. Login shell to enter Linux

Log in with account and password——>/etc/profile (global file G1)——>/etc/profile.d/ (global script directory F1)——> ~/.profile (user file U1)——> ~/. bashrc (user file U2) -->/etc/bash.bashrc (global file G2)

When a user logs in to the Linux system, the Shell will be started as the login shell. The sequence of loading environment variables by the login shell at this time is as shown in the figure above.

After a user logs in to the system, the /etc/profile global environment variable file will first be loaded. This is the default Shell main environment variable file on the Linux system. This file will be loaded for every user login on the system.

After the /etc/profile file is loaded, the script file in the /etc/profile.d/ directory will be executed. There are many script files in this directory, as shown below, so that the user can run the script after logging in.
Insert image description here

Then start running ~/.profile (user environment variable file). In this file, you will find ~/.bashrc (user environment variable file). If there is one, it will be executed. If not, it will not be executed. In ~/ The .bashrc file will look for /etc/bash.bashrc (global environment variable file). If it exists, it will be executed. If it does not exist, it will not be executed.

3.2. Entering Linux without a login shell

Login or remote SSH connection that does not require entering a password --> ~/.bashrc (user file U2) -->/etc/bash.bashrc (global file G2)

If the user's Shell is not started when logging in (such as starting when manually typing bash or other logins and remote SSH connections that do not require entering a password), then this non-login Shell will only load ~/.bashrc (user environment variable file) , and will find /etc/bash.bashrc (global environment variable file).

  • Therefore, if you want to be able to read the set environment variables and other contents under a non-login shell, you need to write the variable settings into ~/.bashrc or /etc/bashrc instead of ~/.bash_profile or /etc/profile.

Guess you like

Origin blog.csdn.net/weixin_43693967/article/details/128352089