在 Debian(以及绝大多数 Linux 发行版)里,环境变量(env)的“优先级”主要取决于变量的来源/作用范围以及Shell 的读取顺序。下面按**从低到高(后者覆盖前者)**的顺序说明。
/etc/environmentexport示例:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
✅ 所有用户生效
❌ 不依赖 shell
/etc/profile/etc/profile.d/*.sh/etc/profile.d/*.sh/etc/profile 自动加载/etc/bash.bashrc~/.profile~/.bashrc~/.bash_profile / ~/.bash_login~/.profile加载顺序:
~/.bash_profile > ~/.bash_login > ~/.profile
~/.bashrcexport PATH=/tmp:$PATH
✅ 当前 shell 及子进程
❌ 退出即失效
env 命令临时执行env PATH=/usr/bin command
✅ 只对该命令生效
/etc/environment
↓
/etc/profile
↓
/etc/profile.d/*
↓
/etc/bash.bashrc
↓
~/.profile
↓
~/.bash_profile / ~/.bash_login
↓
~/.bashrc
↓
命令行 export
↓
env 命令
❌ 以为 ~/.bashrc 一定比 /etc/environment 晚加载
✅ 是的,但 非交互 shell 不一定读 .bashrc
❌ cron / systemd 会读 .bashrc
✅ 不会,需要显式设置
echo $PATH
env | sort
bash -xl
如果你有具体场景(比如 systemd / cron / Docker / sudo),我可以给你更精确的优先级说明。