To add an environment variable:
Method One:
$ VI ~/.BASHRC
Add Path= $PATH on the last line:/my_new_path:
$source ~/.BASHRC
Method Two: Note: Modify the environment variable path. Specific: Modify the/ETC/BASH.BASHRC file (available to all users)
$sudo VIM/ETC/BASH.BASHRC
Add at the end of the file:
Export path= $PATH:/my_new_path:
After modifying the path, turn off the terminal and reopen the terminal (or restart)
Background information: (reproduced)
Environment variables are closely related to the shell, and the user logs on to the system and launches a shell. It's usually bash for Linux, but it can be reset or switched to another Shell. For UNIX, it could be cshelll. Environment variables are set by shell commands, and set environment variables can be used by all current users running programs. For bash, the shell program can use variable names to access the appropriate environment variables, and export to set environment variables. This is illustrated by a few examples below.
1) Etc/profile:This filefor each user of the systemSets the environment information that is executed when the user logs on for the first time.
and collect the shell settings from the configuration file of the/ETC/PROFILE.D directory.
Note: Here we set the global variables that are available to all users.
2)/ETC/BASHRC:Execute this file for each user running the bash shell. When the bash shell is opened, the file is read.
3) ~/.bash_profile:Each user can use this file to enterSpecific shell information for your own use,when the user logs in, the file is only executed once!By default, he sets some environment variables to execute the user's. bashrc file.
Note: ~ Under Linux is the variable representing the home.
In addition, under different Linux operating systems, this file may be different and may be ~/.bash_profile; ~/.bash_login or ~/.profile one or more of them, if there are several, then the order of execution is: ~/.bash_profile, ~/.bash_login, ~/. Profile For example, I use Ubuntu, my User folder, the default is only the ~/.profile file.
4) ~/.BASHRC:The file containsBash letter dedicated to your bash shellThe file is read when you log on and each time you open a new shell.
So how do we add our own defined environment variables?
Open the file with Notepad, and then write it down in the end:
Xiaokang=kangkang
and then save it so that each time we open a new terminal, we have this variable in effect. Remember, if you've already opened a terminal, and then you've modified the file, it won't work in this terminal. In general, the user is best to make changes here, but sometimes it overrides the parent variable, such as path is root, but if you write path=xx in this file, So in the future all paths have become xx, so we should write in this file as:
path= $PATH: xx
This adds the original and your own. And note that the Linux system uses: Split to represent side by side, not Windo;
3 and 4 are all in the user directory, and their only difference is:. bash_ Profile can only be started once when logging in. This 3 file does not seem to be in my Ubuntu.
5) ~/.bash_logout: In addition, variables set in/etc/profile (global) can be applied to any user, while variables set in ~/.BASHRC, etc. (local) can only inherit/etc/ The variables in profile, they are \ "parent-child \" relationships. The
~/.bash_profile is interactive, the login way into bash runs
~/.BASHRC is the interactive non-login way into bash
usually the two settings are roughly the same, so the former usually calls the latter.
OK, let's summarize how they do it:
When you log in and the shell is bash, bash first executes the command in the/etc/profile file (if the file exists), and then it looks for the ~/.bash_profile,~/.bash_login or ~/.profile file sequentially, and executes the command found in the first readable file. When login 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 non-interactively to run a shell script, bash looks for the BASH_ENV environment variable to determine the name of the execution file.
The second piece of information
The procedure to execute a file when logging on to Linux is as follows:
When you first log in to Linux, start the/etc/profile file, and then start the ~/.bash_profile in the user directory , ~/.bash_login or ~/.profile file
One (depending on the different Linux operating systems, the naming is not the same!!!!!!!!!! In my Ubuntu only ~/.profile),
The order of execution is: ~/.bash_profile, ~/.bash_login, ~/.profile.
If the ~/.bash_profile file exists, the ~/.BASHRC file is typically executed.
Because the following code is commonly found in the ~/.bash_profile file:
If [-f ~/.BASHRC]; Then
. ./BASHRC
Fi
In ~/.BASHRC, the following code is generally available:
if [-F/ETC/BASHRC]; Then
. /bashrc
Fi
Therefore, ~/.BASHRC will call the/ETC/BASHRC file. Finally, when you exit the shell, the ~/.bash_logout file is also executed.
about the scope of each file, the following instructions were found on the Web:
(1)/etc/profile: This file When the user first logs on, the file is executed. and from/etc/ The configuration file of the PROFILE.D directory collects the shell settings.
(2)/ETC/BASHRC: Executes this file for each user running the bash shell. When the bash shell is opened, the file is read.
(3) ~/.bash_profile: Each user can use the file to enter 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.
(4) ~/.BASHRC: This file contains bash information dedicated to your bash shell, which is read when you log on and every time you open a new shell.
(5) ~/.bash_logout: Executes the file each time it exits the system (exiting the bash shell).
In addition, variables set in/etc/profile (global) can be applied to any user, while variables set in ~/.BASHRC, etc. (local) can only inherit/etc/ The variables in profile, they are "parent-child" relationships.
(6) ~/.bash_profile is the interactive, login way into bash run ~/.BASHRC is the interactive non-login way into bash normally the two settings are roughly the same, so the former usually calls the latter.
The use of various environment variable settings files such as/etc/profile and/etc/environment
First add the export LANG=ZH_CN to/etc/profile, exit the system to log in again, login prompt display in English.
Delete the export lang=zh_cn in/etc/profile, add LNAG=ZH_CN to/etc/environment, exit the system to log in again, sign in prompt to display Chinese.
The user environment is established by always executing/etc/profile and then reading the/etc/environment. Why is it as different as the above?
The/etc/environment should be executed first and then executed/etc/profile.
/etc/environment is the environment that sets up the entire system, and/etc/profile is the environment for setting up all users, regardless of the logged-on user, which is related to the logged-on user.
The execution of the system application can be irrelevant to the user environment, but it is related to the system environment, so when you log in, you see the prompt information, such as date, time information display format is related to the system environment Lang, default Lang=en_us, if the system environment LANG=ZH_CN , the prompt message is in Chinese, otherwise it is in English.
For a user's shell initialization, the/etc/profile is executed first, and then the file/etc/environment is read. The/etc/environment is performed first for the entire system. Do you understand that correctly?
/etc/enviroment--/etc/profile-$HOME/.profile--$HOME/.env (if present)
/etc/profile is the environment variable for all users
/etc/enviroment is the environment variable of the system
The order in which the shell reads the system when logging in should be
/etc/profile->/etc/enviroment-$HOME/.profile-$HOME/.env
The reason should be the difference between the user environment and the system environment described by JTW.
If the same variable has different values in the user environment (/etc/profile) and the system environment (/etc/environment), then the user environment should prevail.
Linux environment variables