是否有一个 Bash 文件,无论是登录还是非登录,都将始终以交互模式获取?

S18*_*182 10 bash login bashrc

据我所知,交互shell可能登录也可能不登录,它们的启动文件是不一样的。

  • 如果交互式 + 登录外壳?/etc/profile然后第一个可读的~/.bash_profile, ~/.bash_login, 和~/.profile
  • 如果交互式 + 非登录 shell ?/etc/bash.bashrc然后~/.bashrc

每次使用交互式 shell 时,我都想设置一些变量,无论它是否是登录 shell。

Gil*_*il' 10

不,没有。是的,这是设计缺陷。

在 中使用以下内容~/.bash_profile

if [ -e ~/.profile ]; then . ~/.profile; fi
if [[ -e ~/.bashrc && $- = *i* ]]; then . ~/.bashrc; fi
Run Code Online (Sandbox Code Playgroud)

请注意 bash 有一个更奇怪的怪癖:当它是非交互式登录 shell 并且父进程是rshdor 时sshd,bash 源~/.bashrc(但不是~/.bash_profileor ~/.profile)。所以你可能想把它放在你的顶部.bashrc

if [[ $- != *i* ]]; then return; fi
Run Code Online (Sandbox Code Playgroud)

另见的.bashrc和.bash_profile文件之间的差异登录shell和非登录shell之间的区别是什么?

  • @vonbrand 呃,什么?不,这些怪癖绝对与安全无关。没有涉及“意外文件”(除了在非交互式设置中加载 `.bashrc` 实际上很可能算作意外),也没有对任何安全敏感的上下文进行任何检查。 (2认同)