Table of Contents
Linux environment variable configuration" >Linux environment variable configuration
Linux reads environment variables" >Linux reads environment variables
Linux environment variable configuration method one: export PATH" >Linux environment variable configuration method one: export PATH
Linux环境变量配置方法二:vim ~/.bashrc" >Linux环境变量配置方法二:vim ~/.bashrc
Linux环境变量配置方法三:vim ~/.bash_profile" >Linux环境变量配置方法三:vim ~/.bash_profile
Linux环境变量配置方法四:vim /etc/bashrc" >Linux环境变量配置方法四:vim /etc/bashrc
Linux环境变量配置方法五:vim /etc/profile" >Linux环境变量配置方法五:vim /etc/profile
Linux环境变量配置方法六:vim /etc/environment" >Linux环境变量配置方法六:vim /etc/environment
Linux环境变量加载原理解析" >Linux环境变量加载原理解析
环境变量的分类" >环境变量的分类
测试Linux环境变量加载顺序的方法" >测试Linux环境变量加载顺序的方法
Linux环境变量文件加载详解" >Linux环境变量文件加载详解
一些小技巧" >一些小技巧
Home System Tutorial LINUX When playing with Linux systems and configuring environment variables, you can't avoid these 6 methods!

When playing with Linux systems and configuring environment variables, you can't avoid these 6 methods!

Feb 05, 2024 am 11:36 AM
linux linux tutorial linux system linux command shell script embeddedlinux good promise Getting started with linux linux learning

Linux environment variable configuration

When installing software, you often need to configure environment variables. The following are examples of various environment variable configuration methods.

The following are some environment instructions:

  • System: Ubuntu 14.0
  • Username: uusama
  • You need to configure the MySQL environment variable path: /home/uusama/mysql/bin

玩 Linux 系统,配置环境变量,绕不开这 6 种方法!

Linux reads environment variables

How to read environment variables:

  • 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 execution effects of these two commands are 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
Copy after login

The PATH variable defines the search path for running the command. Use colon: to separate different paths. You can add double quotes or not when using export definition.

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
Copy after login

Precautions:

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

Linux环境变量配置方法二:vim ~/.bashrc

通过修改用户目录下的~/.bashrc文件进行配置:

vim ~/.bashrc

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

注意事项:

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

Linux环境变量配置方法三:vim ~/.bash_profile

和修改~/.bashrc文件类似,也是要在文件最后加上新的路径即可:

vim ~/.bash_profile

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

注意事项:

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

Linux环境变量配置方法四:vim /etc/bashrc

该方法是修改系统配置,需要管理员权限(如root)或者对该文件的写入权限:

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

vim /etc/bashrc

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

注意事项:

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

Linux环境变量配置方法五:vim /etc/profile

该方法修改系统配置,需要管理员权限或者对该文件的写入权限,和vim /etc/bashrc类似:

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

vim /etc/profile

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

注意事项:

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

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

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

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

vim /etc/profile

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

注意事项:

  • 生效时间:新开终端生效,或者手动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"
Copy after login

修改完之后保存,新开一个窗口,然后echo $UU_ORDER观察变量的值:

uusama@ubuntu:~echoUU_ORDER
$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc
Copy after login

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

  1. /etc/environment
  2. /etc/profile
  3. /etc/bash.bashrc
  4. /etc/profile.d/test.sh
  5. ~/.profile
  6. ~/.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
Copy after login

其次再打开~/.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"
Copy after login

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

一些小技巧

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

也可以使用alias命令定义一些命令的别名,比如alias rm=”rm -i”(双引号必须),并把这个代码加入到~/.profile中,这样你每次使用rm命令的时候,都相当于使用rm -i命令,非常方便。

The above is the detailed content of When playing with Linux systems and configuring environment variables, you can't avoid these 6 methods!. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

How to install deepseek How to install deepseek Feb 19, 2025 pm 05:48 PM

There are many ways to install DeepSeek, including: compile from source (for experienced developers) using precompiled packages (for Windows users) using Docker containers (for most convenient, no need to worry about compatibility) No matter which method you choose, Please read the official documents carefully and prepare them fully to avoid unnecessary trouble.

Ouyi okx installation package is directly included Ouyi okx installation package is directly included Feb 21, 2025 pm 08:00 PM

Ouyi OKX, the world's leading digital asset exchange, has now launched an official installation package to provide a safe and convenient trading experience. The OKX installation package of Ouyi does not need to be accessed through a browser. It can directly install independent applications on the device, creating a stable and efficient trading platform for users. The installation process is simple and easy to understand. Users only need to download the latest version of the installation package and follow the prompts to complete the installation step by step.

BITGet official website installation (2025 beginner's guide) BITGet official website installation (2025 beginner's guide) Feb 21, 2025 pm 08:42 PM

BITGet is a cryptocurrency exchange that provides a variety of trading services including spot trading, contract trading and derivatives. Founded in 2018, the exchange is headquartered in Singapore and is committed to providing users with a safe and reliable trading platform. BITGet offers a variety of trading pairs, including BTC/USDT, ETH/USDT and XRP/USDT. Additionally, the exchange has a reputation for security and liquidity and offers a variety of features such as premium order types, leveraged trading and 24/7 customer support.

Get the gate.io installation package for free Get the gate.io installation package for free Feb 21, 2025 pm 08:21 PM

Gate.io is a popular cryptocurrency exchange that users can use by downloading its installation package and installing it on their devices. The steps to obtain the installation package are as follows: Visit the official website of Gate.io, click "Download", select the corresponding operating system (Windows, Mac or Linux), and download the installation package to your computer. It is recommended to temporarily disable antivirus software or firewall during installation to ensure smooth installation. After completion, the user needs to create a Gate.io account to start using it.

Ouyi Exchange Download Official Portal Ouyi Exchange Download Official Portal Feb 21, 2025 pm 07:51 PM

Ouyi, also known as OKX, is a world-leading cryptocurrency trading platform. The article provides a download portal for Ouyi's official installation package, which facilitates users to install Ouyi client on different devices. This installation package supports Windows, Mac, Android and iOS systems. Users can choose the corresponding version to download according to their device type. After the installation is completed, users can register or log in to the Ouyi account, start trading cryptocurrencies and enjoy other services provided by the platform.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Why does an error occur when installing an extension using PECL in a Docker environment? How to solve it? Apr 01, 2025 pm 03:06 PM

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

See all articles