Methods and differences for setting Linux environment variables
Setting Linux environment variables can be implemented through export or by modifying several files. It is necessary to find out the differences between the two methods and these files.
Set Linux environment variables through files
The first step is to set global environment variables, which will take effect for all users:
- Etc/profile: This file sets environment information for each user of the system. When a user logs on, the file is executed once and shell settings are collected from the configuration file in the/etc/profile. d directory. It is generally used to set global variables used by all users.
- /Etc/bashrc: When the bash shell is opened, the file is read. That is to say, each time a terminal shell is opened, the file will be read.
Next, it corresponds to the above two files, but only takes effect for a single user:
- ~ /. Bash_profile or ~ /. Profile: takes effect only for a single user. This file is executed only once when the user logs on. You can use this file to add your own shell variable information. In addition, in different LINUX operating systems, this file may be different ~ /. Bash_profile ,~ /. Bash_login or ~ /. One or more of the profiles. If there are several, the execution sequence is :~ /. Bash_profile ,~ /. Bash_login ,~ /. Profile. For example, the Ubuntu system is generally ~ /. Profile file.
- ~ /. Bashrc: It takes effect only for a single user. When you log on and open a new shell, the file is read.
In addition, you can modify the/etc/environment file to set environment variables. /Etc/environment is also set as a global variable. In terms of the role of the file itself,/etc/environment sets the entire system environment, the/etc/profile parameter sets the environment for all users. Pay attention to the following points:
- The system first reads the etc/profile and then reads the/etc/environment (or vice versa ?)
- /Etc/environment cannot contain commands.
VAR="..."Does not use export.
- Use
source /etc/environmentYou can enable the variable settings to take effect immediately in the current window. You need to log off/restart the settings before they take effect for each new terminal window.
Modifying Linux environment variables
Take Ubuntu as an example to modify ~ /. Profile file:
vim ~/.profile
If the file exists, the following code is displayed at the end of the file. The values of the PATH variable are separated by colons:
# set PATH so it includes user's private bin if it exists
if[-d "$HOME/bin"];then
PATH="$HOME/bin:$PATH"
fi
Add code at the endPATH="$PATH:/usr/local/Hadoop/bin"Note that no space exists on either side of the equal sign (=), that is:
# set PATH so it includes user's private bin if it exists
if[-d "$HOME/bin"];then
PATH="$HOME/bin:$PATH"
fi
PATH="$PATH:/usr/local/hadoop/bin"
This file is read only once upon user login, so it takes effect only after restart (modify/etc/profile and/etc/environment ). However, you can use the commandsource ./.profileMake it take effect immediately. Passecho $PATHYou can see the modified variable value:
source ./.profile
echo $PATH
Use the Shell command export to modify Linux environment variables
Another way to modify the Linux environment variables is to use the Shell command export. Note that the variable name must not have a dollar number $, which must be included in the value assignment statement:
export PATH=$PATH:/usr/local/hadoop/bin
The export method is only valid for the current terminal Shell.: The variable set with export is only valid for the current terminal Shell. That is to say, if a new terminal is opened, the variable set with this export cannot be read in the new terminal. It is suitable for setting some temporary variables.
Select the setting method as needed. For example, variables such as JAVA_HOME can be set as global variables, which can be set in/etc/environment.
This article permanently updates the link address: