When customizing software installation, it is often necessary to configure environment variables. Below are various ways to configure environment variables.
In the following example, the environment we use is as follows:
The following is how to view environment variables:
export
command to display all environment variables defined by the current system. echo $PATH
command to output the current value of the PATH
environment variable. The effect of executing these two commands is as follows:
uusama@ubuntu:~$ export declare -x HOME="/home/uusama" declare -x LANG="en_US.UTF-8" declare -x LANGUAGE="en_US:" declare -x LESSCLOSE="/usr/bin/lesspipe %s %s" declare -x LESSOPEN="| /usr/bin/lesspipe %s" declare -x LOGNAME="uusama" declare -x MAIL="/var/mail/uusama" declare -x PATH="/home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" declare -x SSH_TTY="/dev/pts/0" declare -x TERM="xterm" declare -x USER="uusama" uusama@ubuntu:~$ echo $PATH /home/uusama/bin:/home/uusama/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The PATH
variable defines the search path for running the command. Use colons :
to separate different paths. You can add double quotes when defining using export
Don’t add it.
使用export
命令直接修改PATH
的值,配置MySQL进入环境变量的方法:
export PATH=/home/uusama/mysql/bin:$PATH # 或者把PATH放在前面 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
$PATH
部分,避免覆盖原来配置通过修改用户目录下的~/.bashrc
文件进行配置:
vim ~/.bashrc # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
source ~/.bashrc
生效PATH
定义,则可能不生效和修改~/.bashrc
文件类似,也是要在文件最后加上新的路径即可
vim ~/.bash_profile # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
source ~/.bash_profile
生效~/.bash_profile
文件,则可以编辑~/.profile
文件或者新建一个该方法是修改系统配置,需要管理员权限(如root)或者对该文件的写入权限:
# 如果/etc/bashrc文件不可编辑,需要修改为可编辑 chmod -v u+w /etc/bashrc vim /etc/bashrc # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
source /etc/bashrc
生效该方法修改系统配置,需要管理员权限或者对该文件的写入权限,和vim /etc/bashrc
类似:
# 如果/etc/profile文件不可编辑,需要修改为可编辑 chmod -v u+w /etc/profile vim /etc/profile # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
source /etc/profile
生效该方法是修改系统环境配置文件,需要管理员权限或者对该文件的写入权限:
# 如果/etc/bashrc文件不可编辑,需要修改为可编辑 chmod -v u+w /etc/environment vim /etc/profile # 在最后一行加上 export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
source /etc/environment
takes effectVarious configuration methods of environment variables are listed above, so how does Linux load these configurations? In what order are they loaded?
Specific loading order will cause environment variable definitions with the same name to be overwritten or not take effect.
Environment variables can be simply divided into user-defined environment variables and system-level environment variables.
~/.bashrc
, ~/.profile
(some systems are: ~/.bash_profile
) /etc/bashrc
, /etc/profile
(some systems are: /etc/bash_profile
), /etc /environment
In addition, in the user environment variables, the system will first read the ~/.bash_profile
(or ~/.profile
) file. If there is no such file, it will read it. ~/.bash_login
, and then read ~/.bashrc
based on the contents of these files.
为了测试各个不同文件的环境变量加载顺序,我们在每个环境变量定义文件中的第一行都定义相同的环境变量UU_ORDER
,该变量的值为本身的值连接上当前文件名称。
需要修改的文件如下:
在每个文件中的第一行都加上下面这句代码,并相应的把冒号后的内容修改为当前文件的绝对文件名。
export UU_ORDER="$UU_ORDER:~/.bash_profile"
修改完之后保存,新开一个窗口,然后echo $UU_ORDER
观察变量的值:
uusama@ubuntu:~$ echo $UU_ORDER $UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc
可以推测出Linux加载环境变量的顺序如下:
由上面的测试可容易得出Linux加载环境变量的顺序如下,:
系统环境变量 -> 用户自定义环境变量
/etc/environment -> /etc/profile -> ~/.profile
打开/etc/profile
文件你会发现,该文件的代码中会加载/etc/bash.bashrc
文件,然后检查/etc/profile.d/
目录下的.sh
文件并加载。
# /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "$PS1" ]; then if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1='\h:\w\$ ' if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1='# ' else PS1='$ ' fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi
其次再打开~/.profile
文件,会发现该文件中加载了~/.bashrc
文件。
# if running bash if [ -n "$BASH_VERSION" ]; then # include .bashrc if it exists if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc" fi fi # set PATH so it includes user's private bin directories PATH="$HOME/bin:$HOME/.local/bin:$PATH"
从~/.profile
文件中代码不难发现,/.profile
文件只在用户登录的时候读取一次,而/.bashrc
会在每次运行Shell
脚本的时候读取一次。
You can customize an environment variable file, such as defining uusama.profile
under a certain project. Use export
to define a series of variables in this file, and then Add:
sourc uusama.profile after the ~/.profile
file, so that you can use a series of variables defined by yourself in the Shell script every time you log in.
You can also use the alias
command to define aliases for some commands, such as alias rm="rm -i"
(double quotes are required), and add this code to ~/.profile
, so every time you use the rm
command, it is equivalent to using the rm -i
command, which is very convenient.
The above is the detailed content of A complete guide to configuring Linux environment variables, beginners must know.... For more information, please follow other related articles on the PHP Chinese website!