在 Linux 系统中,环境变量的设置可以分布在多个配置文件中,具体取决于不同的 Shell 类型(如 bash、zsh 等)以及作用范围(用户级/系统级)。以下是主要配置文件及其作用:
/etc/environmentKEY=value 格式,不支持变量扩展或脚本。/etc/profile/etc/profile.d/*.sh/etc/profile 会加载此目录下的所有 .sh 脚本,推荐将自定义全局配置放在这里。~/.bash_profile 或 ~/.bash_login 或 ~/.profile~/.bashrc~/.zshrc(Zsh 用户)~/.bashrc。~/.pam_environmentbash
export MY_VAR="value"su - username):
/etc/profile → ~/.bash_profile (~/.bash_login → ~/.profile)~/.bashrc# 查看所有环境变量
printenv
# 或
env
# 查看特定变量(如 PATH)
echo $PATH
bash
source ~/.bashrc # 或对应配置文件~/.profile)。~/.xsession 或 ~/.xinitrc 加载变量。根据你的需求选择合适的文件进行修改!