登录和非登录shell之间读取的conf文件是什么?

sam*_*sam 5 shell configuration

我发现那些是.bash_profile, .bashrc, .bash_login, .profile

他们之间的阅读顺序是什么?

Mik*_*kel 8

基本上,如果它是一个登录 shell,/etc/profile那么它会提供.bash_profile. 如果它不是登录 shell,但您在终端,那么它/etc/bash.bashrc会提供.bashrc.

但实际上要复杂得多。

我阅读手册页的方式:

if bash_mode; then
    if login_shell; then
        if test -e /etc/profile; then source /etc/profile; fi
        if test -e .bash_profile; then source .bash_profile
        elif test -e .bash_login; then source .bash_login
        elif test -e .profile; then source .profile; fi
    elif interactive_shell || remote_shell; then
        if test -e /etc/bash.bashrc; then source /etc/bash.bashrc
        if test -e .bashrc; then source .bashrc; fi
    elif test -n "$BASH_ENV"; then
        source "$BASH_ENV"
    fi
elif sh_mode; then
    if login_shell; then
        if test -e /etc/profile; then source /etc/profile; fi
        if test -e .profile; then source .profile; fi
    elif interactive_shell; then
         if test -n "$ENV"; then
             source "$ENV"
         fi
    fi
fi
Run Code Online (Sandbox Code Playgroud)

每当 shell 以-bash(注意减号)或-l选项运行时,它都是一个登录 shell 。这通常发生在您使用login命令(Linux 虚拟控制台执行此操作)、通过 ssh 登录或终端模拟器启用了“登录外壳”选项时。

它是一个交互式 shell,任何时候标准输入是一个终端,或者 bash 是用该-i选项启动的。请注意,如果 shell 也是登录 shell,bash 不会检查 shell 是否是交互式的。因此,.bash_profile通常包含 source 的代码.bashrc,因此您可以在交互式 shell 和登录 shell 之间共享相同的设置。