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

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

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

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

答案1

基本上,如果它是一个登录 shell,/etc/profile那么它会获取.bash_profile.如果它不是登录 shell,但您位于终端,它会获取/etc/bash.bashrcthen .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

任何时候 shell 作为-bash(注意减号)或使用-l选项运行时都是登录 shell。当您使用命令(Linux 虚拟控制台执行此操作)、通过 ssh 登录login或您的终端仿真器启用了“登录 shell”选项时,通常会发生这种情况。

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

相关内容