search
HomeSystem TutorialLINUXA complete guide to configuring Linux environment variables, beginners must know...
A complete guide to configuring Linux environment variables, beginners must know...Feb 05, 2024 pm 01:00 PM
linuxlinux tutoriallinux systemlinux commandshell scriptembeddedlinuxgood promiseGetting started with linuxlinux learning

When customizing software installation, it is often necessary to configure environment variables. Below are various ways to configure environment variables.

Linux 环境变量配置全攻略,初学者必会…

In the following example, the environment we use is as follows:

  • Operating system: Ubuntu 14.0
  • Username: uusama
  • The path to which MySQL environment variables need to be configured: /home/uusama/mysql/bin

View environment variables in Linux

The following is how to view environment variables:

  • Use the export command to display all environment variables defined by the current system.
  • Use the 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.

Linux环境变量配置方法一: export PATH

使用export命令直接修改PATH的值,配置MySQL进入环境变量的方法:

export PATH=/home/uusama/mysql/bin:$PATH

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

注意事项:

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

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

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

vim ~/.bashrc

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

注意事项:

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

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

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

vim ~/.bash_profile

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

注意事项:

  • 生效时间:使用相同的用户打开新的终端时生效,或者手动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

注意事项:

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

注意事项:

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

注意事项:

  • Effective time: Open a new terminal to take effect, or manually source /etc/environment takes effect
  • Validity period: Permanent
  • Effective scope: Valid for all users

Linux environment variable loading principle analysis

Various 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.

Classification of environment variables

Environment variables can be simply divided into user-defined environment variables and system-level environment variables.

  • User-level environment variable definition files: ~/.bashrc, ~/.profile (some systems are: ~/.bash_profile)
  • System-level environment variable definition files: /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.

测试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观察变量的值:

uusama@ubuntu:~$ echo $UU_ORDER

$UU_ORDER:/etc/environment:/etc/profile:/etc/bash.bashrc:/etc/profile.d/test.sh:~/.profile:~/.bashrc

可以推测出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

其次再打开~/.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脚本的时候读取一次。

Some tips

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!

Statement
This article is reproduced at:良许Linux教程网. If there is any infringement, please contact admin@php.cn delete
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

手机远程linux工具有哪些手机远程linux工具有哪些Apr 29, 2022 pm 05:30 PM

手机远程linux工具有:1、JuiceSSH,是一款功能强大的安卓SSH客户端应用,可直接对linux服务进行管理;2、Termius,可以利用手机来连接Linux服务器;3、Termux,一个强大的远程终端工具;4、向日葵远程控制等等。

linux中lsb是什么意思linux中lsb是什么意思May 07, 2022 pm 05:08 PM

linux中,lsb是linux标准基础的意思,是“Linux Standards Base”的缩写,是linux标准化领域中的标准;lsb制定了应用程序与运行环境之间的二进制接口,保证了linux发行版与linux应用程序之间的良好结合。

See all articles

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)