在 CentOS 中,env 配置“不生效”通常是因为配置位置不对、作用范围不对、或没有重新加载。下面按常见场景给你系统梳理一下。
export MY_VAR=123
env | grep MY_VAR
✅ 只在当前终端会话生效
❌ 关掉终端 / 新开 shell 就失效
常见文件:
~/.bashrc~/.bash_profile~/.profile~/.bashrc,但用的是 非交互 shell(如 cron、systemd)sourceecho 'export MY_VAR=123' >> ~/.bash_profile
source ~/.bash_profile
CentOS 登录 shell 默认读
~/.bash_profile,不读~/.bashrc(除非手动 source)
文件:
/etc/profile/etc/profile.d/*.sh/etc/environmentvim /etc/profile.d/myenv.sh
内容:
export MY_VAR=123
然后:
source /etc/profile
source /etc/profile
# 或
source ~/.bash_profile
这些环境 不会读取 bashrc/profile
[Service]
Environment=MY_VAR=123
或
EnvironmentFile=/etc/sysconfig/myenv
MY_VAR=123
* * * * * /path/script.sh
检查是否有:
export MY_VAR=abc
在更靠后的文件中又写了一次
比如 zsh、sh
echo $SHELL
~/.zshrcecho $MY_VAR
which env
bash -l -c 'echo $MY_VAR'
CentOS 中 env 不生效,90% 是因为:文件写错 / 没 source / 非登录 shell / systemd 不读 profile
如果你能告诉我:
我可以直接给你精确配置方案。