Environment variables are closely related to Shell. A Shell is started after the user logs in to the system. For Linux, this is usually bash, but it can also be reset or switched to other shells. For UNIX, probably CShelll. Environment variables are set through Shell commands, and the set environment variables can be used by all programs run by the current user. For the shell program bash, you can access the corresponding environment variables through variable names, and set the environment variables through export. Below are several examples to illustrate.
1)etc/profile: This file sets environment information for each user of the system. When the user logs in for the first time, this file is executed.
And collects shell information from the configuration file in the /etc/profile.d directory Settings.
Note: Here we set global variables that are available to all users.
2)/etc/bashrc: Execute this file for each user running the bash shell. When the bash shell is opened, this file is read.
3)~/.bash_profile: Each user can use this The file input is dedicated to shell information for your own use. When the user logs in, the file is only executed once! By default, he sets some environment variables and executes the user's .bashrc file.
Note: ~ under LINUX represents the HOME variable of.
In addition, under different LINUX operating systems, this file may be different, it may be ~/.bash_profile; one or more of ~/.bash_login or ~/.profile. If there are several, then execute The order is: ~/.bash_profile, ~/.bash_login, ~/.profile. For example, I am using Ubuntu, and by default there is only the ~/.profile file in my user folder.
4)~/.bashrc: This file contains bash information specific to your bash shell. This file is read when logging in and every time you open a new shell.
(Note: This file starts with . , so it is hidden in the folder)
So how do we add our own defined environment variables?
Open this file with Notepad, and then write at the end:
xiaokang=kangkang
Then save it, so that every time you open a new terminal, our variable will take effect. Remember, if you have a terminal open and you modify this file, it will not take effect in this terminal. In general, it is best for users to make modifications here, but sometimes the parent variables will be overwritten. For example, PATH is set by ROOT, but if you write PATH=xx in this file, then all PATHs in the future will become xx. , so we should write it in this file as:
PATH=$PATH:xx
In this way, the original one is added together with your own. And please note that under LINUX system, use: split to mean parallel, not windo;
3 and 4 are both in the user directory. The only difference between them is: .bash_profile can only be started once when logging in. These 3 files don't seem to exist in my Ubuntu.
5)~/.bash_logout: Execute this file every time you exit the system (exit the bash shell).
In addition, the variables (global) set in /etc/profile can affect any user, and ~/ The variables (local) set in .bashrc, etc. can only inherit the variables in /etc/profile. They have a "father-son" relationship.
~/.bash_profile is used to enter bash operation in interactive and login mode
~/.bashrc It is an interactive non-login way to enter bash operation. Usually the settings of the two are roughly the same, so the former will usually call the latter.
When you log in and the login shell is bash, bash first executes the commands in the /etc/profile file (if the file exists), and then it sequentially looks for ~/.bash_profile, ~/.bash_login or ~/.profile file, and execute the commands in the first readable file found. When logged in bash exits, it will execute the commands in the ~/.bash_logout file.
When starting an interactive bash When launched non-interactively to run a shell script, bash will look for the bash_env environment variable to determine the name of the file to be executed.
Second information
-------------------------------------------------- ------------------------------------
When you first log in to Linux, first start the /etc/profile file, and then start ~/.bash_profile in the user directory; one of the ~/.bash_login or ~/.profile files (depending on different Linux operating systems, The naming is different! ! ! ! In my Ubuntu, the order is: ~/.bash_login.
If the ~/.bash_profile file exists, the ~/.bashrc file will generally be executed.
Because there is usually the following code in the ~/.bash_profile file:
if [ -f ~/.bashrc ] ; then
. ./bashrc
fi
~/.bashrc, there is usually the following code:
if [ -f /etc/bashrc ] ; then
. /bashrc
fi
So, ~/.bashrc will call the /etc/bashrc file. Finally, when exiting the shell, the ~/.bash_logout file is also executed.
The execution sequence is: /etc/profile -> (~/.bash_profile | ~/.bash_login | ~/.profile) -> ~/.bashrc -> /etc/bashrc -> ~/.bash_logout
Regarding the scope of each file, I found the following description on the Internet:
(1)/etc/profile: This file sets environment information for each user of the system. When the user logs in for the first time, this file is executed. And from Collect shell settings from the configuration file in the /etc/profile.d directory.
(2)/etc/bashrc: Execute this file for each user running the bash shell. When the bash shell is opened, this file is read.
(3)~/.bash_profile: Each user can use this file to enter shell information dedicated to their own use. When the user logs in, the file is only executed once! By default, he sets some environment variables and executes the user's .bashrc file.
(4)~/.bashrc: This file contains bash information specific to your bash shell. This file is read when logging in and every time a new shell is opened.
(5)
~/.bash_logout: Execute this file every time you exit the system (exit the bash shell).
In addition, the variables (global) set in /etc/profile can affect any user, and~ Variables (local) set in /.bashrc, etc. can only inherit variables in
/etc/profile. They have a "father-son" relationship.
(6) ~/.bash_profile is used to enter bash running in interactive and login mode. ~/.bashrc is used to enter bash running in interactive non-login mode. Usually the settings of the two are roughly the same, so the former will usually call the latter.
The usefulness of various environment variable setting files such as /etc/profile and /etc/environment
First add export to /etc/profile, log out of the system and log in again. The login prompt will be displayed in English.
Delete the export in /etc/profile, add LNAG=zh_CN to /etc/environment, log out of the system and log in again, the login prompt will display in Chinese.
In the process of establishing the user environment, /etc/profile is always executed first and then /etc/environment is read. Why are there differences as described above?
You should execute /etc/environment first and then /etc/profile.
/etc/environment is to set the environment of the entire system, while /etc/profile is to set the environment of all users. The former has nothing to do with the logged in user, and the latter is related to the logged in user.
The execution of system applications may have nothing to do with the user environment, but is related to the system environment. So when you log in, the prompt information you see, such as the display format of date and time information, is related to the LANG of the system environment. , the default LANG=en_US, if the system environment LANG=zh_CN, the prompt information is in Chinese, otherwise it is in English.
For the user's SHELL initialization, /etc/profile is executed first, and then the file /etc/environment is read. For the entire system, /etc/environment is executed first. Is this correct understanding?
/etc/enviroment --> /etc/profile --> $HOME/.profile -->$HOME/.env (if it exists)
/etc/profile is the environment variable for all users
/etc/ enviroment is the environment variable of the system
The order read by the shell when logging in to the system should be
/etc/profile ->/etc/enviroment -->$HOME/.profile -->$HOME/.env
The reason should be This is the difference between user environment and system environment that jtw said. If the same variable has different values in the user environment (/etc/profile) and the system environment (/etc/environment), then the user environment should prevail.