Ubuntu Environment Variables--Add and remove

Source: Internet
Author: User

Turn from: http://beanocean.diandian.com/post/2013-11-09/40060047963


Note:

1. The author's system is Ubuntu 13.10, where environment variables are set in similar but different ways in other Linux distributions;

2. Here take the environment variable path as an example, environment variables can also have a lot, or you can define their own names. What is an environment variable

Environment variables generally refer to some of the parameters used in the operating system to specify the operating system environment, such as temporary folder location and system folder location, etc. [1]. Since some parameters can be used as the default settings for the system, there is no need to specify these parameters when running a program. For example, you always use a program, when used on the command line, every time you run this program you have to enter the directory of the program to start the program. If you add the directory of the program to the environment variable, you just need to enter the name of the program when you run the program, and the system will automatically search for the location of your program without the problem of command not found. How to set environment variables

First of all, we need to know a few documents related to environment variables [2]:

/etc/profile--This file sets the environment information for each user of the system, and the file is executed the first time the user logs on. And collects the shell's settings from the/ETC/PROFILE.D directory configuration file;

/etc/environment--the second file that the operating system uses at logon, the system sets environment variables for the environment file before reading your own profile;

/etc/bashrc--executes this file for each user running the bash shell. When the bash shell is opened, the file is read;

~/.profile--Each user can use this file to enter shell information that is specific to their own use, and the file executes only once when the user logs on. By default, it sets some environment variables to execute the user's. bashrc file;

~/.bashrc--The file contains bash information dedicated to your bash shell, which is read when you log in and each time you open a new shell;

After you know the above 5 files, we have a way to set up the environment variables.

Method One: Directly modify the/etc/enviroment file, the scope of this method is global, permanent.

Open the/etc/environment file, which reads as follows:

Path= "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

Add the path you want to add in the environment variable path.

Method Two: Modify the/etc/profile file, the scope of this method is global, permanent.

This file is not a file that holds environment variable information, and when you log on, the system simply sets the environment variables according to its contents.

Export path= $PATH: [Your Path1]:[your path2]:[...]
Export Path=[your path1]:[your path2]:[...]: $PATH

where each path is separated by a colon, $PATH refers to the environment variable before adding your path. I feel like this is a unique way of defining a computer, just as I = i + 1.

Method Three: Modify/ETC/BASHRC or ~/.BASHRC files

These two files are also not intended to save environment variables, but are set only when the bash shell is used, so the method is set to the same as in two:

Export path= $PATH: [Your Path1]:[your path2]:[...]
Export Path=[your path1]:[your path2]:[...]: $PATH

For/ETC/BASHRC files, modifications are applied to the user, but for the ~/.BASHRC file, the modification only works for the current user. The effect of this modification is also permanent.

In addition, there is a question that I am not very clear: these two files are just settings for the environment variables of the bash shell, and I feel disconnected from the bash shell, without using the command line, such modifications are meaningless. This idea is right I am not very clear, I have a shallow understanding of the bash shell, this place is doubtful, and later on the system to understand and solve.

Method Four: Modify the ~/.profile file

This approach is essentially the same as modifying/etc/profile, which simply modifies the current user's own configuration file. So the effect is limited to the current user, but it is also permanent.

Method Five: Use shell command in terminal

Export path= $PATH: [Your Path1]:[your path2]:[...]
Export Path=[your path1]:[your path2]:[...]: $PATH

This method is temporary shutdown of the terminal This setting is also invalid, and the effect is limited to the current terminal, the other terminals this setting does not start.

After the settings are successful, you can use the command to view the settings environment variables.

ENV//Print all environment variables

echo $PATH//print PATH environment variable

Then we can see the relevant changes after the PATH variable. In the top four methods, the changes do not set to take effect immediately (think about why), need to reboot or log on again (which requires a reboot, which requires a login). , of course, using the source command is also OK. Use the method:

source [file name]  //Don't forget the path, or execute it under the file path

How to delete changes to an environment variable

Restore the configuration file changes in the above method back. In addition, since the fifth method is temporary and localized, it is only necessary to close this terminal reference

[1] Environment variables, Baidu Encyclopedia

[2] Ubuntu environment variable, XIAOSU_521,CSDN


Turn from: http://beanocean.diandian.com/post/2013-11-09/40060047963


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.