Introduction to the method of setting environment variables under Linux

Source: Internet
Author: User

The export command needs to be written to a system file, and the files that have this feature are often as follows:

/etc/environment or/etc/profile or ~/.profile or/ETC/BASH.BASHRC or ~/.BASHRC.

There are so many to use, what is the difference between them, who first?

First, take a look at what these files are all about:

1./etc/environment–> is the first file that the system reads at logon to set environment variables for all processes. The system uses this file not to execute the commands in this file, but to assign values to the key based on the code in the Key=value mode, so if you want to define the PATH environment variable in the file, just add a line like path= $PATH:/xxx/bin code.

2./etc/profile–> is the second file that is executed when the system logs on, and can be used to set environment variables for all users of the system.

3.~/.profile–> is a profile file that corresponds to the current logged-on user to customize the current user's personal work environment.

4./etc/bash.bashrc–> is a bash initialization file for all users, and the environment variables set in this will be applied to all users ' shells, which will be executed once each time the shell is opened by the user.

5.~/.bashrc–> is a bash initialization file that corresponds to the current logged-on user, and the system executes the file once every time the user opens the shell.

According to the above description, these documents should be executed in order:

/etc/enviroment–>/etc/profile–>~/.profile–>/etc/bash.bashrc–> ~/.BASHRC

To verify that this order is correct, here is a small experiment, assuming that we are logged in with the username xyz. To add a line to the/etc/environment:

Env_msg= "This is/etc/environment"

This means adding an environment variable env_msg, and then adding two lines of code to the/etc/profile:

echo $ENV _msg >>/home/xyz/log.txt

echo "This Is/etc/profile" >>/home/xyz/log.txt

Thus, if the/etc/environment is read by the system before the profile, then the evn_msg value and this is/etc/profile will be printed in the/home/xyz/log.txt.

Add a line of code to/home/xyz/.profile:

echo "This are. Profile" >>/home/xyz/log.txt

Add a line of code to/ETC/BASH.BASHRC:

echo "This IS/ETC/BASH.BASHRC" >>/home/xyz/log.txt

Add a line of code to/HOME/XYZ/.BASHRC:

echo "This is. BASHRC" >>/home/xyz/log.txt

Then, restart the computer to see what the Log.txt file will look like.

After you start your computer and log on as an XYZ user and immediately open/home/xyz/log.txt, you can see the following three line messages in the file:

This is/etc/environment

This is/etc/profile

This are. Profile

This means that the system sequentially reads the contents of/etc/enviroment,/etc/profile, and ~/.profile during the startup of the login.

Then open a shell window, and the log file will add two lines of messages:

This IS/ETC/BASH.BASHRC

This is. BASHRC

This means that in the process of opening the shell, the system executes/ETC/BASH.BASHRC and ~/.BASHRC sequentially. If you close the Shell window and open a new shell window again, the log file adds another two lines to the same message. It can be learned that each time a new shell is opened, the system repeats the two files without moving the contents of the first three files.

Next we open the/etc/environment, change the line just written to env_msg= "This is Not/etc/environment", and then log off and log in as XYZ again, and the result will find three more lines in the log file:

This is not/etc/environment

This is/etc/profile

This are. Profile

This also can be seen, logoff login will also cause the system to the three files read and execute.

However, if you press CTRL+ALT+F1, and then log on to XYZ, then the log file will be more out of the following lines, what is this?

This IS/ETC/BASH.BASHRC

This is/etc/environment

This is/etc/profile

This is. BASHRC

This are. Profile

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.