(Recommended) for setting environment variables in Linux (UBUNTU), refer:

Source: Internet
Author: User

Today, a small error occurred in cross-compiling environment variable settings, which prompted my terminal to encounter a path error, which made me feel a little deeper, first, I will share with you the summary I have seen on the Internet.

Environment variables are closely related to shell. A shell is started after the user logs on to the system. For Linux, bash is generally used, but you can reset or switch to another shell. For UNIX, it may be cshelll. Environment variables are set through shell commands. The configured environment variables can be used by all programs run by the current user. For the bash shell program, the corresponding environment variables can be accessed through the variable name, and the environment variables can be set through export. The following describes several instances.

1) etc/profile: This file sets environment information for each user in the system. When the user logs on for the first time, this file is executed.
And collect shell settings from the configuration file in the/etc/profile. d directory.
Note: here we set global variables that can be used by all users.

2)/etc/bashrc: execute this file for every user running bash shell. When bash shell is opened, the file is read.

3 )~ /. Bash_profile: each user can use this file to input the shell information dedicated to their own use. When a user logs on, this file is only executed once! By default, it sets some environment variables to execute the user's. bashrc file.
Note :~ In Linux, the variable home is represented.
In addition, in different Linux operating systems, this file may be different ~ /. Bash_profile ;~ /. Bash_login or ~ /. One or more of the profiles. If there are several, the execution sequence is :~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile. For example, if I use Ubuntu, only ~ is used by default in my user folder ~ /. Profile file.

4 )~ /. Bashrc: This file contains bash information dedicated to your bash shell. This file is read when you log on 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 the file in notepad, and then write:
Xiaokang = Kangkang
Save the variable so that each time a new terminal is opened, this variable takes effect. Remember, if you have opened a terminal and modified the file, it will not take effect under this terminal. In general, you 'd better modify it here, but sometimes it will overwrite the parent-level variables. For example, if path is set as root, but if you write Path = XX in this file, in the future, all the paths will become XX, so we should write the following in this file:
Path = $ path: xx
In this way, we can add the original and your own. In addition, in Linux, use: to split and represent parallel, rather than windo;
Both 3 and 4 are in the user directory. The only difference is that. bash_profile can only be started once at login. This 3 file does not seem to exist in my ubuntu.

5 )~ /. Bash_logout: execute this file every time you exit the system (exit bash shell.
In addition, the variables (global) set in/etc/profile can act on any user ~ The variables (local) set in/. bashrc can only inherit the variables in/etc/profile. They are \ "Parent-Child \" relationships.

~ /. Bash_profile is interactive and login to run in bash.
~ /. Bashrc runs in bash in interactive non-login mode.
Generally, the two settings are roughly the same, so the former usually calls the latter.

Okay. Let's summarize their execution methods:
When you log on to and log on to shell as Bash, bash first executes the command in the/etc/profile file (if the file exists), and then searches ~ /. Bash_profile ,~ /. Bash_login or ~ /. Profile file, and execute the command in the first readable file found. When you log on to bash and exit, it will execute ~ /. Bash_logout file.
When an interactive Bash is started, it will execute ~ /. Commands in the bashrc file (if the file exists and is readable). When a shell script is started non-interactively, bash searches for the bash_env environment variable and determines the name of the execution file.

Article 2
--------------------------------------------------------------------------------

The file execution process when logging on to Linux is as follows:
When logging on to Linux, start the/etc/profile file first, and then start ~ /. Bash_profile ;~ /. Bash_login or ~ /. One of the profile files (different names vary depending on Linux operating systems !!!!!!!!!! In my Ubuntu, only ~ /. Profile ),

The execution sequence is :~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile.
If ~ /. If the bash_profile file exists, it is generally executed ~ /. Bashrc file.

Because in ~ The/. bash_profile file usually contains the following code:
If [-f ~ /. Bashrc]; then
../Bashrc
Fi
~ /. Bashrc usually has the following code:
If [-F/etc/bashrc]; then
./Bashrc
Fi
So ,~ /. Bashrc calls the/etc/bashrc file. Finally, when you exit the shell, ~ /. Bash_logout file.
The execution sequence is:/etc/profile-> (~ /. Bash_profile | ~ /. Bash_login | ~ /. Profile)-> ~ /. Bashrc->/etc/bashrc-> ~ /. Bash_logout
The scope of each file is described as follows:
(1)/etc/profile: This file sets the environment information for each user in the system. When the user logs on for the first time, this file is executed. and from/etc/profile. d directory configuration file to collect shell settings.
(2)/etc/bashrc: execute this file for every user running bash shell. When bash shell is opened, the file is read.
(3 )~ /. Bash_profile: each user can use this file to input the shell information dedicated to their own use. When a user logs on, this file is only executed once! By default, it sets some environment variables to execute the user's. bashrc file.
(4 )~ /. Bashrc: This file contains bash information dedicated to your bash shell. This file is read when you log on and every time you open a new shell.
(5)
~ /. Bash_logout: execute this file every time you exit the system (exit bash shell.
In addition, the variables (global) set in/etc/profile can act on any user ~ /. The variables (local) set in bashrc and so on can only inherit
The variables in/etc/profile are "Parent-Child" relationships.
(6 )~ /. Bash_profile is interactive and login to run bash ~ /. Bashrc is the interactive non-login mode that enters Bash. Generally, the two settings are roughly the same, so the former usually calls the latter.
Use of Environment Variable setting files such as/etc/profile and/etc/environment
Add export lang = zh_cn to/etc/profile, log out of the system, and log on again. The logon prompt is in English.
Delete export lang = zh_cn in/etc/profile, add lnag = zh_cn to/etc/environment, log out of the system and log on again. The logon prompt displays Chinese characters.
When the user environment is established, the/etc/profile is always executed first and then the/etc/environment is read. Why is there a difference as described above?
Run/etc/environment first and then/etc/profile.
/Etc/environment is used to set the environment of the entire system, while/etc/profile is used to set the environment of all users. The former has nothing to do with the login user, and the latter has to do with the login user.
The execution of system applications may be unrelated to the user environment, but related to the system environment. Therefore, when you log on, you will see the prompt information, the display format of date and time information is related to the Lang in the system environment. The default is lang = en_us. If the system environment lang = zh_cn, the message is displayed in Chinese, otherwise, it is in English.
For Shell initialization, run/etc/profile first and then read the file/etc/environment. For the whole system, run/etc/environment first. Is this true?
/Etc/enviroment -->/etc/profile --> $ home/. profile --> $ home/. ENV (if any)
/Etc/profile is the environment variable of all users
/Etc/enviroment is the system environment variable
The sequence of shell reading when logging on to the system should be
/Etc/profile->/etc/enviroment --> $ home/. profile --> $ home/. env
The reason should be the difference between the user environment and the system environment mentioned by jtw.
If the same variable has different values in the user environment (/etc/profile) and system environment (/etc/environment), the user environment should prevail.

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.