


6 ways to configure Linux environment variables, which one is smoother?
Linux environment variable configuration
When customizing software installation, it is often necessary to configure environment variables. The following lists various ways to configure environment variables. .
The environment description for all examples below is as follows:
System: Ubuntu 14.0 Username: uusama Need to configure MySQL environment variable path:/home/uusama/mysql/bin
Linux reads environment variables
How to read environment variables:
The export command displays all environment variables defined by the current system echo $PATH command outputs the value of the current PATH environment variable
The effect of executing these two commands is as follows
uusama@ubuntu:~exportdeclare -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 colon: to separate different paths. When using export definition, you can add double quotes or not. add.
Linux environment variable configuration method one: export PATH
Use the export command to directly modify the value of PATH and configure MySQL to enter the environment variable method:
export PATH=/home/uusama/mysql/bin:PATH# 或者把PATH放在前面export PATH=PATH:/home/uusama/mysql/bin
Notes:
生效时间:立即生效 生效期限:当前终端有效,窗口关闭后无效 生效范围:仅对当前用户有效 配置的环境变量中不要忘了加上原来的配置,即$PATH部分,避免覆盖原来配置
Linux环境变量配置方法二:vim ~/.bashrc
通过修改用户目录下的~/.bashrc文件进行配置:
vim ~/.bashrc# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
生效时间:使用相同的用户打开新的终端时生效,或者手动source ~/.bashrc生效 另外,搜索公众号Linux就该这样学后台回复“Linux”,获取一份惊喜礼包。 生效期限:永久有效 生效范围:仅对当前用户有效 如果有后续的环境变量加载文件覆盖了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/bashrcvim /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/profilevim /etc/profile# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
生效时间:新开终端生效,或者手动source /etc/profile生效 生效期限:永久有效 生效范围:对所有用户有效
Linux环境变量配置方法六:vim /etc/environment
该方法是修改系统环境配置文件,需要管理员权限或者对该文件的写入权限:
# 如果/etc/bashrc文件不可编辑,需要修改为可编辑chmod -v u+w /etc/environmentvim /etc/profile# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
注意事项:
生效时间:新开终端生效,或者手动source /etc/environment生效 生效期限:永久有效 生效范围:对所有用户有效
Linux环境变量加载原理解析
上面列出了环境变量的各种配置方法,那么Linux是如何加载这些配置的呢?是以什么样的顺序加载的呢?
牛逼啊!接私活必备的 N 个开源项目!赶快收藏
特定的加载顺序会导致相同名称的环境变量定义被覆盖或者不生效。
环境变量的分类
环境变量可以简单的分成用户自定义的环境变量以及系统级别的环境变量。
用户级别环境变量定义文件:~/.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文件并加载。另外,搜索公众号Linux就该这样学后台回复“Linux”,获取一份惊喜礼包。
# /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, using export to define a series of variables in this file, and then following the ~/.profile file Add: source uusama.profile, 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 required), and add this code to ~/.profile, so that you use rm every time When issuing commands, it is equivalent to using the rm -i command, which is very convenient.
The above is the detailed content of 6 ways to configure Linux environment variables, which one is smoother?. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

Docker process viewing method: 1. Docker CLI command: docker ps; 2. Systemd CLI command: systemctl status docker; 3. Docker Compose CLI command: docker-compose ps; 4. Process Explorer (Windows); 5. /proc directory (Linux).

Troubleshooting steps for failed Docker image build: Check Dockerfile syntax and dependency version. Check if the build context contains the required source code and dependencies. View the build log for error details. Use the --target option to build a hierarchical phase to identify failure points. Make sure to use the latest version of Docker engine. Build the image with --t [image-name]:debug mode to debug the problem. Check disk space and make sure it is sufficient. Disable SELinux to prevent interference with the build process. Ask community platforms for help, provide Dockerfiles and build log descriptions for more specific suggestions.

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

The reasons for the installation of VS Code extensions may be: network instability, insufficient permissions, system compatibility issues, VS Code version is too old, antivirus software or firewall interference. By checking network connections, permissions, log files, updating VS Code, disabling security software, and restarting VS Code or computers, you can gradually troubleshoot and resolve issues.

VS Code is available on Mac. It has powerful extensions, Git integration, terminal and debugger, and also offers a wealth of setup options. However, for particularly large projects or highly professional development, VS Code may have performance or functional limitations.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

How to back up VS Code configurations and extensions? Manually backup the settings file: Copy the key JSON files (settings.json, keybindings.json, extensions.json) to a safe location. Take advantage of VS Code synchronization: enable synchronization with your GitHub account to automatically back up all relevant settings and extensions. Use third-party tools: Back up configurations with reliable tools and provide richer features such as version control and incremental backups.
