环境变量是Linux系统全局的,shell变量是当前shell运行过程中保存的变量
环境变量是在系统启动加载的:环境变量分为两种一种是系统环境变量(所有用户共享的),另一种的用户环境变量(根据登陆用户
来加载的)
在shell中查看环境变量命令: printenv
添加shell变量到环境变量:
(1) 添加到当前shell进程的环境变量: export 变量名
(2) 添加到当前用户环境变量: 编辑.bashrc文件,添加export指令导出
(3)添加到系统环境变量:
第一步:在/etc/profile.d 目录下创建 xxx.sh文件
第二步:在xxx.sh文件添加export导出变量
mango@mango:~$ sudo touch /etc/profile.d/mango_env.sh
mango@mango:~$ sudo chmod +x /etc/profile.d/mango_env.sh
mango@mango:~$ sudo nano /etc/profile.d/mango_env.sh
系统加载环境变量过程,就是执行一系列.sh文件过程:
(1)/etc/profile
(2)~/.bash_profile
(3)~/.bash_login
(4)~/.profile
(5) ~/.bashrc
(6) /etc/bashrc
(7)/etc/bash.bashrc
(8) /etc/profile.d/*.sh
Ubuntu shell 执行过程:
第一步:执行 /etc/profile 文件
(1) 如果当前linux使用bash程序作为shell, 就会执行 /etc/bash.bashrc 文件
(2) 加载 /etc/profile.d/* 下sh文件为 所有用户添加环境变量,因此可以用来保存系统环境变量 ;
(3) /etc/bashrc(ubuntu为 /etc/bash.bashrc)
为每一个运行 bash shell 的用户执行此文件。当 bash shell 被打开时,该文件被读取。如果你想对所有的使用 bash 的用户修改某个配置并在以后打开的 bash 都生效的话可以修改这个文件,修改这个文件不用重启,重新打开一个 bash 即可生效,Ubuntu没有此文件,与之对应的是/ect/bash.bashrc。
第二步:为登录用户执行 ~/.bash_profile(ubuntu为 ~/.profile) 文件,该文件仅仅执行一次!默认情况下,它设置一些环境变量,同时执行用户的~/ .bashrc 文件
第三步,每次打开用户打开shell,都会执行 ~/.bashrc
第四步, ~/.bash_logout:
当每次退出系统(退出 bash shell)时,执行该文件。可把一些清理工作的命令放到这文件中。
保存系统环境变量推荐在 /etc/profile.d/*sh 里添加, 保存用户环境变量推荐在 ~/.bashrc里添加,这个文件是用户专属的