ubuntu

ubuntu环境下env变量优先级

小樊
34
2025-08-13 11:02:10
栏目: 智能运维

在Ubuntu环境下,环境变量的优先级如下:

  1. 本地配置文件

    • ~/.bashrc:针对单个用户的配置。
    • /etc/bash.bashrc:系统级的配置,适用于所有用户。
  2. 登录Shell配置文件

    • ~/.profile:针对单个用户的登录Shell配置。
    • /etc/profile:系统级的登录Shell配置,适用于所有用户。
  3. 交互式Shell配置文件

    • ~/.bash_profile:针对单个用户的交互式Shell配置,通常会source ~/.bashrc
    • /etc/bash.bash_profile:系统级的交互式Shell配置。
  4. 系统级配置文件

    • /etc/environment:系统级的环境变量配置,适用于所有用户和进程。
  5. 启动脚本

    • /etc/profile.d/目录下的脚本:这些脚本会在登录时执行,可以用来设置环境变量。
  6. 当前Shell会话

    • 在当前Shell会话中直接使用export命令设置的环境变量。

优先级从高到低依次为:

  1. 当前Shell会话中直接使用export命令设置的环境变量。
  2. /etc/environment
  3. /etc/profile.d/目录下的脚本。
  4. /etc/profile
  5. ~/.bash_profile(如果存在且source了~/.bashrc)。
  6. ~/.profile
  7. /etc/bash.bashrc
  8. ~/.bashrc

需要注意的是,~/.bash_profile~/.profile通常不会同时存在,因为~/.bash_profile会source ~/.bashrc。如果两者都存在,~/.bash_profile的优先级更高。

此外,/etc/environment是一个特殊的文件,它设置的环境变量是全局的,且不会被子进程继承。因此,在某些情况下,它的优先级可能会高于其他配置文件。

0
看了该问题的人还看了