search
HomeOperation and MaintenanceLinux Operation and MaintenanceLinux environment variable configuration summary

export PATH
  • vim ~/.bashrc
  • vim ~/.bash_profile
  • vim /etc/bashrc
  • vim /etc/profile
  • vim /etc/environment
  • export命令显示当前系统定义的所有环境变量echo $PATH命令输出当前的PATH环境变量 的值这两个命令执行的效果如下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
    其中PATH变量定义了运行命令的查找路径,以冒号:分割不同的路径,使用export定义的时候可加双引号也可不加。推荐下自己做的 Spring Cloud 的实战项目:https://github.com/YunaiV/onemall03、Linux环境变量配置方法一:export PATH使用export命令直接修改PATH的值,配置MySQL进入环境变量的方法:export PATH=/home/uusama/mysql/bin:$PATH# 或者把PATH放在前面export PATH=$PATH:/home/uusama/mysql/bin
    注意事项:生效时间:立即生效生效期限:当前终端有效,窗口关闭后无效生效范围:仅对当前用户有效配置的环境变量 中不要忘了加上原来的配置,即$PATH部分,避免覆盖原来配置04、Linux环境变量配置方法二:vim ~/.bashrc通过修改用户目录下的~/.bashrc文件进行配置:vim ~/.bashrc# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
    注意事项:生效时间:使用相同的用户打开新的终端时生效,或者手动source ~/.bashrc生效生效期限:永久有效生效范围:仅对当前用户有效如果有后续的环境变量加载文件覆盖了PATH定义,则可能不生效05、Linux环境变量配置方法三:vim ~/.bash_profile和修改~/.bashrc文件类似,也是要在文件最后加上新的路径即可:vim ~/.bash_profile# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
    注意事项:生效时间:使用相同的用户打开新的终端时生效,或者手动source ~/.bash_profile生效生效期限:永久有效生效范围:仅对当前用户有效如果没有~/.bash_profile文件,则可以编辑~/.profile文件或者新建一个06、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生效生效期限:永久有效生效范围:对所有用户有效07、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中文社区回复关键字"私房菜”获取一份惊喜礼包。
    08、Linux环境变量配置方法六:vim /etc/environment该方法是修改系统环境配置文件,需要管理员权限或者对该文件的写入权限:# 如果/etc/bashrc文件不可编辑,需要修改为可编辑chmod -v u+w /etc/environmentvim /etc/profile# 在最后一行加上export PATH=$PATH:/home/uusama/mysql/bin
    注意事项:生效时间:新开终端生效,或者手动source /etc/environment生效生效期限:永久有效生效范围:对所有用户有效09、Linux环境变量加载原理解析上面列出了环境变量的各种配置方法,那么Linux是如何加载这些配置的呢?是以什么样的顺序加载的呢?特定的加载顺序会导致相同名称的环境变量 定义被覆盖或者不生效。10、环境变量的分类环境变量可以简单的分成用户自定义的环境变量以及系统级别的环境变量。用户级别环境变量 定义文件:~/.bashrc、~/.profile(部分系统为:~/.bash_profile)系统级别环境变量 定义文件:/etc/bashrc、/etc/profile(部分系统为:/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~/.bash_login, and then read ~/.bashrc based on the contents of these files.

11. Method for testing the loading order of Linux environment variables

In order to test the environment variable loading order of different files, we set the load order of each environment variable The first line in the definition file all defines the same environment variable UU_ORDER. The value of this variable is its own value connected to the current file name.

The files that need to be modified are as follows:

  • /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

12、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  fifiif [ -d /etc/profile.d ]; then  for i in /etc/profile.d/*.sh; do    if [ -r $i ]; then      . $i    fi  done  unset ifi

其次再打开~/.profile文件,会发现该文件中加载了~/.bashrc文件。

# if running bashif [ -n "$BASH_VERSION" ]; then    # include .bashrc if it exists    if [ -f "$HOME/.bashrc" ]; then . "$HOME/.bashrc"    fifi# set PATH so it includes user's private bin directoriesPATH="$HOME/bin:$HOME/.local/bin:$PATH"

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

13、一些小技巧

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

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

<br/>
  声明:本文部分素材转载自互联网,如有侵权立即删除 。


The above is the detailed content of Linux environment variable configuration summary. 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: A Look at Its Fundamental StructureLinux: A Look at Its Fundamental StructureApr 16, 2025 am 12:01 AM

The basic structure of Linux includes the kernel, file system, and shell. 1) Kernel management hardware resources and use uname-r to view the version. 2) The EXT4 file system supports large files and logs and is created using mkfs.ext4. 3) Shell provides command line interaction such as Bash, and lists files using ls-l.

Linux Operations: System Administration and MaintenanceLinux Operations: System Administration and MaintenanceApr 15, 2025 am 12:10 AM

The key steps in Linux system management and maintenance include: 1) Master the basic knowledge, such as file system structure and user management; 2) Carry out system monitoring and resource management, use top, htop and other tools; 3) Use system logs to troubleshoot, use journalctl and other tools; 4) Write automated scripts and task scheduling, use cron tools; 5) implement security management and protection, configure firewalls through iptables; 6) Carry out performance optimization and best practices, adjust kernel parameters and develop good habits.

Understanding Linux's Maintenance Mode: The EssentialsUnderstanding Linux's Maintenance Mode: The EssentialsApr 14, 2025 am 12:04 AM

Linux maintenance mode is entered by adding init=/bin/bash or single parameters at startup. 1. Enter maintenance mode: Edit the GRUB menu and add startup parameters. 2. Remount the file system to read and write mode: mount-oremount,rw/. 3. Repair the file system: Use the fsck command, such as fsck/dev/sda1. 4. Back up the data and operate with caution to avoid data loss.

How Debian improves Hadoop data processing speedHow Debian improves Hadoop data processing speedApr 13, 2025 am 11:54 AM

This article discusses how to improve Hadoop data processing efficiency on Debian systems. Optimization strategies cover hardware upgrades, operating system parameter adjustments, Hadoop configuration modifications, and the use of efficient algorithms and tools. 1. Hardware resource strengthening ensures that all nodes have consistent hardware configurations, especially paying attention to CPU, memory and network equipment performance. Choosing high-performance hardware components is essential to improve overall processing speed. 2. Operating system tunes file descriptors and network connections: Modify the /etc/security/limits.conf file to increase the upper limit of file descriptors and network connections allowed to be opened at the same time by the system. JVM parameter adjustment: Adjust in hadoop-env.sh file

How to learn Debian syslogHow to learn Debian syslogApr 13, 2025 am 11:51 AM

This guide will guide you to learn how to use Syslog in Debian systems. Syslog is a key service in Linux systems for logging system and application log messages. It helps administrators monitor and analyze system activity to quickly identify and resolve problems. 1. Basic knowledge of Syslog The core functions of Syslog include: centrally collecting and managing log messages; supporting multiple log output formats and target locations (such as files or networks); providing real-time log viewing and filtering functions. 2. Install and configure Syslog (using Rsyslog) The Debian system uses Rsyslog by default. You can install it with the following command: sudoaptupdatesud

How to choose Hadoop version in DebianHow to choose Hadoop version in DebianApr 13, 2025 am 11:48 AM

When choosing a Hadoop version suitable for Debian system, the following key factors need to be considered: 1. Stability and long-term support: For users who pursue stability and security, it is recommended to choose a Debian stable version, such as Debian11 (Bullseye). This version has been fully tested and has a support cycle of up to five years, which can ensure the stable operation of the system. 2. Package update speed: If you need to use the latest Hadoop features and features, you can consider Debian's unstable version (Sid). However, it should be noted that unstable versions may have compatibility issues and stability risks. 3. Community support and resources: Debian has huge community support, which can provide rich documentation and

TigerVNC share file method on DebianTigerVNC share file method on DebianApr 13, 2025 am 11:45 AM

This article describes how to use TigerVNC to share files on Debian systems. You need to install the TigerVNC server first and then configure it. 1. Install the TigerVNC server and open the terminal. Update the software package list: sudoaptupdate to install TigerVNC server: sudoaptinstalltigervnc-standalone-servertigervnc-common 2. Configure TigerVNC server to set VNC server password: vncpasswd Start VNC server: vncserver:1-localhostno

Debian mail server firewall configuration tipsDebian mail server firewall configuration tipsApr 13, 2025 am 11:42 AM

Configuring a Debian mail server's firewall is an important step in ensuring server security. The following are several commonly used firewall configuration methods, including the use of iptables and firewalld. Use iptables to configure firewall to install iptables (if not already installed): sudoapt-getupdatesudoapt-getinstalliptablesView current iptables rules: sudoiptables-L configuration

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)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

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.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor