首页 > 系统相关 >Linux的环境变量.bash_profile .bashrc profile文件、Login Shell和Non login shell

Linux的环境变量.bash_profile .bashrc profile文件、Login Shell和Non login shell

时间:2022-11-24 16:12:36浏览次数:59  
标签:profile Non shell etc bashrc Shell login bash

login shell和non-login shell的区别:

  • login shell:取得bash时需要完整的登录流程。就是说通过输入账号和密码登录系统,此时取得的shell称为login shell。用户成功登陆后使用的是 Login shell,例如,当你通过终端、SSH 或使用 “su - 用户名” 命令来切换账号时都会使用 Login Shell。
    【注】“su 用户名”使用的是non-login shell
  • non-login shell:取得sbash接口的方法不需要重复登录的举动。如以X Window登录登录linux后,再以X的图形界面启动终端机,此时那个终端机并没有需要输入账号和密 码,那个bash环境就是non-login shell。在原本的bash环境下再次执行bash命令,同样也没有输入账号密码就进入新的bash环境(前一个bash的子进程),新的bash也是non-login shell。

在终端输入echo $0,如果执行命令的结果类似 "-bash" 或 “-su”,那么你当前处于 login shell(确认有 “-” 前缀符号)

Login Shell 会执行如下的前置脚本(参考:链接):

  • 执行文件 /etc/profile。bash在读取/etc/profile后会读取各账户的个人配置文件,所读取的个人配置文件主要有三个:~/.bash_profile、~/.bash_login、~/.profile。其实bash在读取的时候只会读取的上述三个文件的其中一个,而读取 的顺序则就是上面的顺序。也就是说bash会依次查找上述三个个人配置文件,且找到一个后,后续的文件便不再读取。
  • 执行 /etc/profile.d/ 目录下所有脚本
  • 执行 ~/.bashrc
  • ~/.bashrc 执行文件 /etc/bashrc

Non login shell 执行如下脚本来初始 shell 环境:

  • 先执行 ~/.bashrc
  • ~/.bashrc 会执行 /etc/bashrc
  • /etc/bashrc 会调用 /etc/profile.d 中脚本

当以non-login shell登录时,如果希望获取用户的环境变量,执行"source ~/.bash_profile"即可(.bash_profile根据情况也可能是.bash_login或.profile),而其中source可以用小数点替换,即"source ~/.bash_profile"等价于". ~/.bash_profile"。source命令的解释可以自行搜索。(参考:链接

所以我认为新增环境变量时,修改~/.bashrc即可

标签:profile,Non,shell,etc,bashrc,Shell,login,bash
From: https://www.cnblogs.com/codingbigdog/p/16922087.html

相关文章