据我所知,交互式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 并且父进程是rshd
or时sshd
,bash 源~/.bashrc
(但不是~/.bash_profile
or ~/.profile
)。所以你可能想把它放在你的顶部.bashrc
:
if [[ $- != *i* ]]; then return; fi
也可以看看.bashrc 和 .bash_profile 之间的区别和登录 Shell 和非登录 Shell 之间的区别?