Set environment variables in Ubuntu

Source: Internet
Author: User

When setting environment variables in linux, if you only need to use them temporarily, you can directly use the set or export command in shell to set the environment variables, if you want this environment variable to be automatically set every time you start the system or open the shell, instead of manually setting it every time, you need to write the export command into a system file, the following are common files with this function:
/Etc/environment or/etc/profile or ~ /. Profile or/etc/bash. bashrc or ~ /. Bashrc.
What are the differences between so many available products? Who should be the first?

First, let's see what these files do:
1./etc/environment-> is the first file read by the system upon logon. It is used to set environment variables for all processes. When using this file, the system does not execute the commands in this file, but assigns values to the KEY according to the code in KEY = VALUE mode. Therefore, if you want to define the PATH environment variable in the file, you only need to add a line of code like PATH = $ PATH:/xxx/bin.
2./etc/profile-> is the second file executed during system logon. It can be used to set environment variables for all users throughout the system.
3 .~ /. Profile-> is the profile file corresponding to the current Login User. It is used to customize the personal work environment of the current user.
4. /etc/bash. bashrc-> is a bash initialization file for all users. The environment variables set here will be applied to the shells of all users. This file will be executed once each time the user opens the shell.
5 .~ /. Bashrc-> is the bash initialization file corresponding to the currently logged-on user. The system will execute this file every time the user opens the shell.

According to the preceding descriptions, the execution sequence of these files should be:
/Etc/enviroment->/etc/profile-> ~ /. Profile->/etc/bash. bashrc-> ~ /. Bashrc
To verify whether the order is correct, we can perform a small test here, assuming that the user name we log on to is xyz. Add a line to/etc/environment:
ENV_MSG = "this is/etc/environment"
In this way, an environment variable ENV_MSG is added, and two lines of code are added to/etc/profile:
Echo $ ENV_MSG>/home/xyz/log.txt
Echo "this is/etc/profile">/home/xyz/log.txt
In this way, If/etc/environment is read by the system before profile, in/home/xyz/log.txt, The EVN_MSG value and this is/etc/profile messages are printed successively.
Add a line of code to/home/xyz/. profile:
Echo "this is. 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 looks like.
After the computer is started, log on with the xyz user and open/home/xyz/log.txt immediately. The following three lines of messages are displayed in the file:
This is/etc/environment
This is/etc/profile
This is. profile
This indicates that the system reads/etc/enviroment,/etc/profile, and ~ /. Profile content.
Open a shell window, and the log file will add two lines of messages:
This is/etc/bash. bashrc
This is. bashrc
This indicates that when the shell is opened, the system runs/etc/bash. bashrc and ~ /. Bashrc. If the shell window is closed and a new shell window is opened again, two lines of the same message will be added to the log file. From this we can know that each time a new shell is opened, the system will repeat the two files without moving the contents of the three files.
Next, open/etc/environment, change the row you just written to env_msg = "This Is Not/etc/environment", log out, and log on to XYZ again, the log file contains three more lines:
This is not/etc/environment
This is/etc/profile
This is. Profile
As you can see, the system will read and execute the three files after you log out and log on again.
However, if you press CTRL + ALT + F1 and then log on to XYZ, the following lines will be displayed in the log file. What is the problem?
This is/etc/bash. bashrc
This is/etc/environment
This is/etc/profile
This is. bashrc
This is. 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.