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

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

据我所知,交互式shell可能是登录的,也可能是未登录的,而且它们的启动文件也不同。

  • 如果交互式 + 登录 shell →/etc/profile那么第一个可读的~/.bash_profile, ~/.bash_login, 和~/.profile
  • 如果是交互式+非登录shell→/etc/bash.bashrc那么~/.bashrc

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

答案1

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

使用以下内容~/.bash_profile

if [ -e ~/.profile ]; then . ~/.profile; fi
if [[ -e ~/.bashrc && $- = *i* ]]; then . ~/.bashrc; fi

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

if [[ $- != *i* ]]; then return; fi

也可以看看.bashrc 和 .bash_profile 之间的区别登录 Shell 和非登录 Shell 之间的区别?

相关内容