Home >System Tutorial >LINUX >Master Linux environment variables to easily improve work efficiency

Master Linux environment variables to easily improve work efficiency

WBOY
WBOYforward
2024-02-11 14:18:18607browse

In Linux, environment variables are a very important concept. They can help us easily manage the configuration information of the system and applications. However, many people's understanding of Linux environment variables is limited to some simple commands and syntax, without an in-depth understanding of their principles and usage. If you want to better master application development and system management under the Linux platform, then it is very necessary to master the use of environment variables.

When customizing the installation of software, you usually need to configure environment variables. Listed below are the different ways to configure environment variables.

How to read environment variables in Linux:

· Use the export command to display all environment variables defined by the current system.

· Use the echo $PATH command to output the value of the current PATH environment variable.

The functions of these two commands are as follows:

export:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                              
⚡ export
CMAKE_ROOT=/Applications/CMake.app/Contents/bin/
COLORTERM=truecolor
DBUS_SESSION_BUS_ADDRESS='unix:path=/run/user/1000/bus,guid=95984122dede7a7f5360af3a642734c0'
DBUS_STARTER_ADDRESS='unix:path=/run/user/1000/bus,guid=95984122dede7a7f5360af3a642734c0'
DBUS_STARTER_BUS_TYPE=session
DESKTOP_SESSION=ubuntu-wayland
DISPLAY=:0
GDMSESSION=ubuntu-wayland
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_SETUP_DISPLAY=:1
......
掌握 Linux 环境变量,轻松提升工作效率

echo $PATH:

linuxmi@linuxmi /home/linuxmi/www.linuxmi.com                              
⚡ echo $PATH
/Applications/CMake.app/Contents/bin/:/home/linuxmi/.nvm/versions/node/v19.3.0/bin:
/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/linuxmi/.cargo/bin:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
:/snap/bin
掌握 Linux 环境变量,轻松提升工作效率

Among them, the PATH variable defines the search path for running the command, and uses colon ":" to separate different paths. When defining using the export command, you can add double quotes or not.

Linux environment variable configuration method one: export PATH

Use the export command to directly modify the value of PATH and add the JDK environment variable:

[root@k8s-node04 JDK]# pwd
/usr/local/JDK
[root@k8s-node04 JDK]# ll
total 4
drwxr-xr-x  9 root root  126 Sep  7 15:21 jdk-11.0.16
drwxr-xr-x. 7   10  143  245 Oct  6  2018 jdk1.8.0_191
-rwxrwxrwx. 1 root root 2277 Mar 15  2019 tomcat.keystore
[root@k8s-node04 JDK]# 
[root@k8s-node04 JDK]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/JDK/jdk-11.0.16/bin:/root/bin
[root@k8s-node04 JDK]# 
[root@k8s-node04 JDK]# export PATH=/usr/local/JDK/jdk1.8.0_191/bin:$PATH
[root@k8s-node04 JDK]# 
[root@k8s-node04 JDK]# echo $PATH
/usr/local/JDK/jdk1.8.0_191/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/u
sr/local/JDK/jdk-11.0.16/bin:/root/bin

Precautions:

·Effective time: effective immediately

· Validity period: only valid for the current terminal, and will expire after the window is closed

· Effective scope: only valid for the current user

· Don’t forget to add the original configuration, that is, the $PATH part, to the configured environment variables to avoid overwriting the original configuration

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

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

vim ~/.bashrc

# Add on the last line
export PATH=$PATH:/usr/local/JDK/jdk1.8.0_191/bin

Precautions:

· Effective time: It takes effect when the same user opens a new terminal, or manually executes source ~/.bashrc

·Effectiveness period: permanent

· Effective scope: only valid for the current user

· If subsequent environment variable loading files overwrite the PATH definition, it may become invalid

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

Similar to modifying the ~/.bashrc file, you also need to add a new path at the end of the file:

vim ~/.bash_profile

# Add on the last line
export PATH=$PATH:/usr/local/JDK/jdk1.8.0_191/bin

Precautions:

· Effective time: It takes effect when the same user opens a new terminal, or manually executes source /.bash_profile

· Validity period: permanent · Validity scope: only valid for the current user

· If there is no /.bash_profile file, you can edit the ~/.profile file or create a new file

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

This method is to modify the system configuration and requires administrator rights (such as root) or write permissions to the file:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable
chmod -v u+w /etc/bashrc

vim /etc/bashrc

# Add on the last line
export PATH=$PATH:/usr/local/JDK/jdk1.8.0_191/bin

掌握 Linux 环境变量,轻松提升工作效率Precautions:

· Effective time: Open a new terminal to take effect, or manually execute source /etc/bashrc to take effect

·Effectiveness period: permanent

· Effective scope: Valid for all users

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

This method modifies the system configuration and requires administrator rights or write permissions to the file, similar to vim /etc/bashrc:

# If the /etc/profile file is not editable, it needs to be modified to be editable
chmod -v u+w /etc/profile

vim /etc/profile

# Add on the last line
export PATH=$PATH:/usr/local/JDK/jdk1.8.0_191/bin
掌握 Linux 环境变量,轻松提升工作效率

注意事项:

· 生效时间:新开终端生效,或手动执行source /etc/profile生效

· 生效期限:永久生效

· 生效范围:对所有用户有效

Linux环境变量配置方法六:vim /etc/environment

该方法是修改系统环境配置文件,需要管理员权限或对文件的写入权限:

# If the /etc/bashrc file is not editable, it needs to be modified to be editable
chmod -v u+w /etc/environment

vim /etc/environment

# Add on the last line
export PATH=$PATH:/usr/local/JDK/jdk1.8.0_191/bin

注意事项:

· 生效时间:新开终端生效,或手动执行source /etc/environment生效

· 生效期限:永久生效

· 生效范围:对所有用户有效

Linux环境变量加载原理分析

以上列举了环境变量的各种配置方法,那么Linux是如何加载这些配置的呢?它们的加载顺序是怎样的?

特定的加载顺序会导致同名环境变量定义被覆盖或无效。

环境变量的分类 环境变量可以简单地分为用户定义的环境变量和系统级别的环境变量。

  • 用户级环境变量定义文件:/.bashrc、/.profile(有些系统是:~/.bash_profile)
  • 系统级环境变量定义文件:/etc/bashrc、/etc/profile(有些系统是:/etc/bash_profile)、/etc/environment

此外,在用户环境变量中,系统会先读取~/.bash_profile(或~/.profile)文件,如果没有这样的文件,它会读取~/.bash_login,然后根据这些文件的内容读取~/.bashrc。

如何测试Linux环境变量的加载顺序

为了测试不同文件的环境变量加载顺序,我们在每个环境变量定义文件的第一行定义相同的环境变量 UU_ORDER,其值将其自身的值连接到当前文件名后面。

需要修改的文件如下:

/etc/environment
/etc/profile
/etc/profile.d/test.sh,新建文件,无需跳过文件夹
/etc/bashrc,或/etc/bash.bashrc
/.bash_profile,或/.profile ~
/.bashrc

在每个文件的第一行添加以下代码,并根据当前文件的绝对文件名相应地修改冒号后面的内容。

export UU_ORDER="$UU_ORDER:~/.bash_profile"

修改完成后保存,打开一个新的窗口,然后使用echo $UU_ORDER命令观察变量的值:

linuxmi@ubuntu:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:
~/.profile:~/.bashrc

可以推断出,Linux 加载环境变量的顺序如下:

  • /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 文件,可以发现在该文件中加载了 ~/.bashrc 文件。

从 ~/.profile 文件中的代码中,不难发现当用户登录时,只会读取一次 /.profile 文件,而每次运行 Shell 脚本时,都会读取一次 ~/.bashrc。

一些小技巧:

你可以自定义一个环境变量文件,比如在某个项目下定义 uusama.profile,使用 export 定义一系列变量在该文件中,然后在 ~/.profile 文件后面添加 sourc uusama.profile,这样就可以在登录的 Shell 脚本中使用自己定义的一系列变量。

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 that every time you use the rm command , it is very convenient to use the rm -i command.

In Linux systems, environment variables can be said to be everywhere, and almost all applications depend on them. Therefore, mastering the usage methods and techniques of Linux environment variables can not only improve our work efficiency, but also allow us to better understand the mechanism and principles of system operation. I hope this article will be helpful to you and make it easier for you to deal with various tasks and challenges under the Linux platform.

The above is the detailed content of Master Linux environment variables to easily improve work efficiency. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete