总结
环境变量载入分为三段,如图
↓**********调用***************↓
↓*********调用***************↓
/etc/enviroment -> /etc/profile -> ~/.profile ->/etc/.bashrc -> ~/.bashrc
↑ ↑ ↑ ↑ ↑ ↑ ↑开机执行 ↑ ↑ ↑ ↑ ↑ ↑
↑ 登录执行 ↑
↑ ↑每次打开shell执行 ↑ ↑ ↑
- 系统级环境变量
(1)/etc/environment:系统登录时载入的值,用于所有进程,设置系统环境
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
(2)/etc/profile:对系统所有用户设定环境变量,一般会调用/etc/bash.bashrc文件
/etc/bash.bashrc:系统级bashrc文件,每次打开shell时执行一次
这两个文件修改后一般都要重启系统才能生效。
- 用户级环境变量
(1)~/.profile 当前登录用户的环境配置,仅在用户登录时执行一次。一般会设置环境变量和调用用户的.bashrc文件
(2)~/.bashrc 当前用户的bash初始化文件,每次shell打开时都会执行一次,
相关命令
- 查看所有全局变量
env
printenv
printenv HOME
echo $HOME
echo $PATH
- 本shell 中添加临时环境变量
export DOWNLOAD=/home/yan/download
export PATH=/home/yan/anaconda2:$PATH
export PATH=$PATH:/home/yan/anaconda2
- 删除全局变量
unset DOWNLOAD
- profile中添加,或.bashrc文件中添加
$sudo gedit /etc/profile
$sudo gedit /etc/profile
打开文件后添加
export PATH=$PATH:/my_new_path
保存退出,使之生效
source /etc/profile #生效