Linux的环境变量可在多个文件中配置,如/etc/profile,/etc/profile.d/*.sh,/.bashrc,/.bash_profile等,下面说明上述几个文件之间的关系和区别。
bash的运行模式可分为login shell和non-login shell。
例如,我们通过终端,输入用户名、密码,登录系统之后,得到就是一个login shell。而当我们执行以下命令ssh hadoop103 ,在hadoop103执行command的就是一个non-login shell。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-c3mc6aY2-1633428329051)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/edfa14f2-9af7-48d4-9c1d-7ccecf0536f2/Untitled.png)]
这两种shell的主要区别在于,它们启动时会加载不同的配置文件,login shell启动时会加载/etc/profile,~/.bash_profile,~/.bashrc。non-login shell启动时会加载~/.bashrc。
而在加载~/.bashrc(实际是~/.bashrc中加载的/etc/bashrc)或/etc/profile时,都会执行如下代码片段,
因此不管是login shell还是non-login shell,启动时都会加载/etc/profile.d/*.sh中的环境变量。