In Ubuntu 14.04, for example, other Linux distributions are similar, but there are differences. Take path for example here.
Add several locations for environment variables
- /etc/profile-This file sets the environment information for each user of the system, which is executed when the user first logs on. and collects the shell settings from the configuration file of the/ETC/PROFILE.D directory;
- /etc/environment-The second file that the operating system uses when logging on, the system sets environment variables for environment files 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 dedicated to their own use, and when the user logs in, the file executes only once! By default, it sets some environment variables to execute the user's. bashrc file;
- ~/.BASHRC-This file contains bash information dedicated to your bash shell, which is read when you log in and every time you open a new shell;
The action time and scope of the above 5 locations to add environment variables are different.
To add a method:
- Method One: Directly modify the/etc/enviroment file, the scope of this method is global, permanent.
#打开/etc/Environment file with the following contents: PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/ Sbin:/bin:/usr/games:/usr/local/games" #在环境变量PATH中添加你要添加的路径即可.
- Method Two: Modify the/etc/profile file, the scope of this method is global, permanent.
#这个文件不是保存环境变量信息的文件, when logging in, the system simply sets the environment variable based on its content. Export PATH=$PATH: [Your Path1]:[your path2]:[...] Export PATH
#其中, each path is separated by a colon, $PATH refers to the environment variable before adding your path.
- Method Three: Modify the/ETC/BASHRC or ~/.BASHRC file, the two files are not to save the environment variables, but only when using the Bash shell settings, so the Setup method and the same as the second. For the/ETC/BASHRC file, the modification works on the user, but for the ~/.BASHRC file, the modification only works on the current user. The effect of this modification is also permanent.
Export path=$PATH: [Your Path1]:[your path2]:[...] Export PATH
These two files are just settings for the environment variables of the bash shell, leaving the bash shell out of the command line, and this modification may be meaningless.
- Method Four: Modify the ~/.profile file, the role is limited to the current user, but also permanent.
This approach is essentially the same as modifying/etc/profile, which simply modifies the current user's own configuration file. So the function is limited to the current user, but it is also permanent.
- Method Five: Using the shell command in terminal, only works in the current terminal, and closing the current terminal is invalid. Also invalid for other terminal
#在Bash中直接用下面命令: Export PATH=$PATH: [Your Path1]:[your path2]:[...] Export PATH
Display environment variables
- Show all environment variables
Env #打印所有的环境变量
- Display specified environment variables
Echo $PATH #打印PATH环境变量
Let the above changes take effect
In the first four methods, the changes will not be set to take effect immediately (think about the reason), need to restart or re-login (which need to restart, which need to re-login?) ), the fifth method takes effect immediately.
Using the source command also allows the modification to take effect immediately. Use the following method:
SOURCE [ file name] file name refers to the file name of the files modified above
Remove changes to environment variables
Restore the configuration file changes from the above method back. In addition, since the fifth method is temporary and local, it is only necessary to close the terminal.
An example of adding an environment variable
This is a problem I encountered today: Ubuntu 14.04 system installed by default QT4, today I installed QT5, run Qmake time will produce an error. There are many QT commands in the Ubuntu14.04 default ${path} path (specifically a soft connection to Qtchooser, Qtchooser will eventually choose the QT4 command that comes with the system: but these commands do not exist by default) , if ${qtdir}/bin after ${path}, it causes the QT command to fail.
In particular, the Qmake command, in the subsequent installation of the need to use, insurance, in the terminal input qmake, if the display qmake command does not exist, please create a soft connection to resolve.
#安装完成后配置一下PATH, in the. BASHRC (or. zshrc) file in the current user's home directory, add: export qtdir=/opt/qt/5.3/gcc/ export Ld_library_path=${ld_library_path}:${qtdir}/lib export PATH=${qtdir}/bin:$ {PATH} #注意 ${qtdir}/bin and ${path} order! Because there are many QT commands in the/usr/bin in the Ubuntu14.04 default ${path} path(specifically a soft connection to Qtchooser, Qtchooser will eventually choose the QT4 command that comes with the system: but these commands do not exist by default), # If ${qtdir}
#创建软连接 # Soft connection of qmake under/usr/bin to/opt/qt/5.3/gcc/bin/qmake,
#或者将/usr/lib/i386-linux-gnu/qt4/bin/qmake Soft Connect to/opt/qt/5.3/gcc/bin/qmake:sudo Ln -s/opt/qt/5.3/gcc/bin/qmake/usr/bin/qmake or sudo Ln -s/opt/qt/5.3/gcc/bin/qmake/usr/lib/i386-linux-gnu/qt4/bin/qmake #创建完成后再输入qmake查看是否设置成功.
Ubuntu Environment Variables--Add and delete