6 ways to configure Linux environment variables, recommended to collect!
Linux 环境变量配置
在自定义安装软件时,经常需要配置环境变量。下面列举了各种配置环境变量的方法。
下面所有例子的环境说明如下:
- 系统:Ubuntu 14.0
- 用户名:uusama
-
需要配置 MySQL 环境变量路径:/home/uusama/mysql/bin
Linux 读取环境变量
读取环境变量的方法:
-
export
命令显示当前系统定义的所有环境变量 -
echo $PATH
命令输出当前的PATH
环境变量的值
这两个命令执行的效果如下
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
其中PATH
变量定义了运行命令的查找路径,以冒号:
分割不同的路径,使用export
定义的时候可加双引号也可不加。
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
注意事项:
- 生效时间:新开终端生效,或者手动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观察变量的值:
uusama@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文件中代码不难发现,/.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 6 ways to configure Linux environment variables, recommended to collect!. For more information, please follow other related articles on the PHP Chinese website!


For years, Linux software distribution relied on native formats like DEB and RPM, deeply ingrained in each distribution's ecosystem. However, Flatpak and Snap have emerged, promising a universal approach to application packaging. This article exami

The differences between Linux and Windows in handling device drivers are mainly reflected in the flexibility of driver management and the development environment. 1. Linux adopts a modular design, and the driver can be loaded and uninstalled dynamically. Developers need to have an in-depth understanding of the kernel mechanism. 2. Windows relies on the Microsoft ecosystem, and the driver needs to be developed through WDK and signed and certified. The development is relatively complex but ensures the stability and security of the system.

The security models of Linux and Windows each have their own advantages. Linux provides flexibility and customizability, enabling security through user permissions, file system permissions, and SELinux/AppArmor. Windows focuses on user-friendliness and relies on WindowsDefender, UAC, firewall and BitLocker to ensure security.

Linux and Windows differ in hardware compatibility: Windows has extensive driver support, and Linux depends on the community and vendors. To solve Linux compatibility problems, you can manually compile drivers, such as cloning RTL8188EU driver repository, compiling and installing; Windows users need to manage drivers to optimize performance.

The main differences between Linux and Windows in virtualization support are: 1) Linux provides KVM and Xen, with outstanding performance and flexibility, suitable for high customization environments; 2) Windows supports virtualization through Hyper-V, with a friendly interface, and is closely integrated with the Microsoft ecosystem, suitable for enterprises that rely on Microsoft software.

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version
SublimeText3 Linux latest version

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 Mac version
Visual web development tools
