在 CentOS 环境下,环境变量的优先级从高到低如下:
export
命令设置的环境变量。.bashrc
或 .bash_profile
文件中设置的环境变量。.bash_profile
通常在登录 shell 时执行,而 .bashrc
在交互式非登录 shell 时执行。如果 .bash_profile
中有 source ~/.bashrc
语句,那么 .bashrc
中的设置也会生效。/etc/profile
和 /etc/bashrc
中设置的环境变量。/etc/profile
对所有用户生效,而 /etc/bashrc
只对运行 bash 的用户生效。通常情况下,/etc/profile
会 source /etc/bashrc
,以便让全局配置对所有用户生效。.bash_aliases
文件中设置的环境变量,如果 .bashrc
中有 source ~/.bash_aliases
语句,那么 .bash_aliases
中的设置也会生效。/etc/environment
文件设置的环境变量。这个文件在系统启动过程中被读取,对所有用户生效。需要注意的是,不同 shell(如 zsh、fish 等)可能有不同的环境变量配置文件和优先级顺序。上述答案适用于 CentOS 系统下的 bash shell。