[Ubuntu·System·The most complete method of configuring Linux environment variables]

overview

In the Linux environment, configuring environment variables is a common operation to specify the search path for executable programs in the system or user environment.

How to read environment variables

In Linux, you can use the following two commands to read environment variables:

export 命令:用于显示当前系统定义的所有环境变量。
export

Insert image description here

echo $PATH command: used to output the value of the current PATH environment variable.

echo $PATH

Insert image description here

These two commands can help you understand the environment variables defined in the current system and their values.
The following takes configuring MySQL as an example

Linux environment variable configuration method one: export PATH

Use the export command to directly modify the value of PATH and configure the method for MySQL to enter environment variables:

export PATH=/home/uusama/mysql/bin:$PATH
# 或者把PATH放在前面
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:立即生效
生效期限:当前终端有效,窗口关闭后无效
生效范围:仅对当前用户有效
配置的环境变量中不要忘了加上原来的配置,即 $PATH 部分,避免覆盖原来配置

Linux environment variable configuration method two: vim ~/.bashrc

Configure by modifying the ~/.bashrc file in the user directory:

vim ~/.bashrc
# 在最后一行加上
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:使用相同的用户打开新的终端时生效,或者手动 source ~/.bashrc 生效
生效期限:永久有效
生效范围:仅对当前用户有效
如果有后续的环境变量加载文件覆盖了 PATH 定义,则可能不生效

Linux environment variable configuration method three: vim ~/.bash_profile

Similar to modifying the ~/.bashrc file, a new path is also added at the end of the file:

vim ~/.bash_profile
# 在最后一行加上
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:使用相同的用户打开新的终端时生效,或者手动 source ~/.bash_profile 生效
生效期限:永久有效
生效范围:仅对当前用户有效
如果没有 ~/.bash_profile 文件,则可以编辑 ~/.profile 文件或者新建一个

Linux environment variable configuration method four: vim /etc/bashrc

Modifying the system configuration requires administrator rights (such as root) or write permission to the file:

# 如果 /etc/bashrc 文件不可编辑,需要修改为可编辑
sudo chmod u+w /etc/bashrc

sudo vim /etc/bashrc

# 在最后一行加上
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:新开终端生效,或者手动 source /etc/bashrc 生效
生效期限:永久有效
生效范围:对所有用户有效

Linux environment variable configuration method five: vim /etc/profile

Similar to modifying the /etc/bashrc file, administrator rights or write permissions to the file are required:

# 如果 /etc/profile 文件不可编辑,需要修改为可编辑
sudo chmod u+w /etc/profile
sudo vim /etc/profile
# 在最后一行加上
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:新开终端生效,或者手动 source /etc/profile 生效
生效期限:永久有效
生效范围:对所有用户有效

Linux environment variable configuration method six: vim /etc/environment

To modify the system environment configuration file, administrator rights or write permissions to the file are required:

# 如果 /etc/environment 文件不可编辑,需要修改为可编辑
sudo chmod u+w /etc/environment

sudo vim /etc/environment

# 在最后一行加上
export PATH=$PATH:/home/uusama/mysql/bin

Precautions:

生效时间:新开终端生效,或者手动 source /etc/environment 生效
生效期限:永久有效
生效范围:对所有用户有效

Analysis of Linux environment variable loading principle:

In Linux systems, the loading order of environment variables affects the priority and effectiveness order of configurations. The following is the main principle of loading environment variables:

System environment variable classification:
System level: For example, /etc/environment, /etc/profile, /etc/bash.bashrc.
User level: For example, /.bash_profile, /.profile, ~/.bashrc.

Loading order test method:
In different environment variable definition files, add the same variable (such as UU_ORDER) in the first line and assign the value to the current file name.
Observe the changes in variable values ​​through echo $UU_ORDER to infer the loading order.

Speculated Linux environment variable loading order:
/etc/environment
/etc/profile
/etc /bash.bashrc
/etc/profile.d/test.sh
~/.profile
~/.bashrc< /span>
The system will first read the /etc/environment file, and then load other environment variable files in sequence according to the above order. 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 ~/.bash_login, and then read ~/ based on the contents of these files. .bashrc.

This loading sequence can cause environment variable definitions with the same name to be overwritten or not take effect.
Detailed explanation of file loading:
Load the .sh files in the /etc/bash.bashrc and /etc/profile.d/ directories into the /etc/profile file.

if [ -f /etc/bash.bashrc ]; then
  . /etc/bash.bashrc
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 [ -n "BASH_VERSION" ]; then
  if [ -f "HOME/.bashrc" ]; then
    . "HOME/.bashrc"
  fi
fi

Tips

Customize an environment variable file, such as uusama.profile, and add source uusama.profile after ~/.profile to achieve global availability of custom variables.
Use the alias command to define the command alias, such as alias rm="rm -i", and add it to ~/.profile to facilitate the quick use of the command.

Guess you like

Origin blog.csdn.net/weixin_47869094/article/details/134426844