之路 on it Road.com

非登录shell

更简单的是,BASH 只加载 ~/.bashrc,并且再次调用 /etc/bashrc 文件。

# su test
this is ~/.bashrc
this is /etc/bashrc
在 Linux 中BASH Shell 如何加载其配置文件

BASH Shell 将在初始化期间尝试加载多个配置文件。
有 2 组配置文件,具体取决于它是登录shell还是非登录shell。

  • 当我们在裸机监视器上或者通过 SSH 或者使用 # su - [用户名] 登录服务器时,我们将获得一个登录 shell。
  • 当我们在现有会话中的终端(gnome-terminal、另一个内的 shell,或者使用 # su [username]...)启动 shell 时,我们将获得一个非登录 shell。 shell 脚本也在非登录 shell 下执行。

下面我们将通过在这些配置文件中添加一些调试来说明差异。

登录shell

默认情况下,登录 shell 将加载以下文件:

# su - test
this is /etc/profile
this is ~/.bash_profile
this is ~/.bashrc
this is /etc/bashrc

这里注意 ~/.bash_profile 文件,该文件默认调用 ~/.bashrc ,而 ~/.bashrc 调用 /etc/bashrc 。

如果由于某种原因 BASH 没有找到文件 ~/.bash_profile,它会寻找 ~/.bash_login 代替。
如果再次找不到 ~/.bash_login,BASH 将查找 ~/.profile。

在这两种情况下,~/.bashrc 和 /etc/bashrc 不会被加载,除非它们在 ~/.bash_login 或者 ~/.profile 中被明确调用。

~/.bash_profile 不存在时的示例:

存在 ~/.bash_login 时,无论 ~/.profile 是否存在:

$ su - test
Password: 
this is /etc/profile
This is ~/.bash_login

只有 ~/.profile:

# su - test
this is /etc/profile
this is ~/.profile
日期:2020-09-17 00:13:06 来源:oir作者:oir