Linux环境变量配置说明

Linux的环境变量可以在多个文件中配置,如/etc/profile,/etc/profile.d/*.sh,~/.bashrc,~/.bash_profile等,下面说以下上述几个文件之间的关系和区别。

bash的运行模式可分为login shellnon-login shell

例如,我们通过终端,输入用户名、密码,登录系统之后,得到就是一个login shell。而当我们执行以下命令ssh 命令的时候,就是一个non-login shell。

这两种shell的区别在于,它们启动时会加载不同的配置文件,login shell启动时会加载/etc/profile,~/.bash_profile,~/.bashrcnon-login shell启动时会加载~/.bashrc
而在加载~/.bashrc/etc/profile时,都会执行如下代码片段:

因此不管是login shell还是non-login shell,启动时都会加载/etc/profile.d/*.sh中的环境变量。