在 CentOS 系统中,环境变量的优先级通常遵循以下顺序:
本地配置文件:
~/.bashrc
或 ~/.bash_profile
:这些文件是针对单个用户的,通常用于设置用户特定的环境变量。/etc/profile
和 /etc/profile.d/
目录下的脚本:这些文件对所有用户生效,但 /etc/profile.d/
下的脚本会在 /etc/profile
之后执行。系统范围配置文件:
/etc/environment
:这是一个系统范围的配置文件,用于设置全局环境变量。它的优先级高于 /etc/profile
和 /etc/bashrc
。shell 启动文件:
~/.bash_profile
(如果存在)/etc/profile
~/.bashrc
(对于交互式非登录 shell)/etc/bashrc
命令行参数:
VAR=value bash
。这种方式设置的环境变量优先级最高。子进程继承:
总结一下,环境变量的优先级从高到低大致为:
~/.bash_profile
或 /etc/profile
/etc/environment
/etc/profile.d/
下的脚本~/.bashrc
或 /etc/bashrc
需要注意的是,这个优先级顺序可能会因不同的 shell 和系统配置而略有不同。在实际使用中,建议查阅相关文档或使用 env
命令来查看当前的环境变量设置。