Linux environment variable configuration file detailed

Source: Internet
Author: User
Tags readable

Environment variables are closely related to the shell, and the user starts a shell after logging in to the system. It's usually bash for Linux, but it can be reset or switched to another Shell. For UNIX, it may be cshelll. Environment variables are set through shell commands, and set up environment variables that can be used by programs run by all current users. For bash, the shell program can access the corresponding environment variables through the variable name, setting the environment variable by export. Here are several examples to illustrate.

I. System level:

1) 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's configuration file.

Note: Here we set the global variables that are available to all users.

2)/ETC/BASHRC: This file is executed for each user running the bash shell. When the bash shell is opened, the file is read.

Two. User level (these files are in the home directory):

1 ~/.bash_profile: Each user can use this file to enter the shell information dedicated to their own use, when the user logs on, the file is only executed once! By default, he sets some environment variables to execute the user's. bashrc file.

Note: ~ Under Linux is to represent the variable of home.

In addition, under different Linux operating systems, this file may be different, may be ~/.bash_profile; ~/.bash_login or ~/.profile one or several of these, if there are several, then the order of execution is: ~/.bash_profile, ~/.bash_login, ~/.profile. For example, I use Ubuntu, my User folder under the default is only ~/.profile files.

2 ~/.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.

(Note: This file is. Started, so it's hidden in the folder)

So how do we add our own defined environment variables?

Open the file in Notepad and write it in the final:

Xiaokang=kangkang

Then save it so that every time we open a new terminal, we have this variable in effect. Remember, if you have opened a terminal and then you have modified the file, it will not work under this terminal. Generally the user is best to make changes here, but sometimes it overrides the parent variable, for example, the path is set by root, but if you write path=xx in this file, all future path will be XX, so we should write in this file as:

Path= $PATH: XX

This adds the original to your own. Also note in the Linux system using: Split to express side by side, rather than windo;

3 and 4 are in the user directory, their only difference is:. Bash_profile can only be started once when you log on. This 3 file doesn't seem to be in my Ubuntu.

3) ~/.bash_logout: Executes the file every time you exit the system (the Bash shell is exited).

In addition, the variables set in the/etc/profile (global) can be applied to any user, while the variables (local) set in ~/.BASHRC can only inherit the variables in/etc/profile, they are/"parent-child/" relationships.

~/.bash_profile is an interactive, login-style entry into bash to run

~/.BASHRC is the interactive non-login way into bash run

Usually the two settings are roughly the same, so the former usually calls the latter.

Through the above file introduction, you can understand that we need to set the environment variables, the need to modify what files, to all users to be effective, it is necessary to set the system-level environment variables. Conversely, you need to modify the user-level files (preferably, the. profile file, for reasons above).

Also, summarize how they are implemented:

When you log in and the login shell is bash, bash first executes the command in the/etc/profile file (if the file exists), and then it sequentially looks for the ~/.bash_profile,~/.bash_login or ~/.profile file, and executes the command in the first readable file found. When logon bash exits, it executes the commands in the ~/.bash_logout file.

When you start an interactive bash, it executes the commands in the ~/.BASHRC file (if the file exists and is readable). When you start interactively to run a shell script, bash looks for the BASH_ENV environment variable and determines the name of the execution file.

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.