How to Set environment variables in linux

Source: Internet
Author: User

In linux, if you only need to set the environment variables temporarily, you can directly set the environment variables using the set or export command in shell, if you want this environment variable to be automatically set every time you start the system or open the shell, instead of manually setting www.2cto.com each time, you need to write the export command into a system file, common files with this function include:/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 take a look at what these files are doing: 1./etc/environment-> is the first file read by the system at login, used to set environment variables for all processes. When using this file, the system does not execute the command 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, a small test can be performed here, assuming that the username we log on to is xyz. Add one line to/etc/environment: ENV_MSG = "this is/etc/environment". In this way, an environment variable ENV_MSG is added, then add two lines of code to/etc/profile: echo $ ENV_MSG>/home/xyz/log.txt echo "this is/etc/profile">/home/xyz/log.txt, if/etc/environment is read by the system before profile, the EVN_MSG value and this is/etc/profile messages are printed in/home/xyz/log.txt. In/home/xyz /. add a line of code to profile: echo "this is. profile ">/home/xyz/log.txt in/etc/bash. add a line of code to bashrc: echo "this is/etc/bash. bashrc ">/home/xyz/log.txt in/home/xyz /. add a line of code to bashrc: echo "this is. after bashrc ">/home/xyz/log.txt, 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 message are displayed in the file: this is/etc/environmentthis is/etc/profilethis is. profile 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 message: this is/etc/bash. bashrcthis is. bashrc indicates that the system runs/etc/bash in sequence during shell startup. 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/environmentthis is/etc/profilethis is. 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. bashrcthis is/etc/environmentthis is/etc/profilethis is. bashrcthis 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.