4

I found that those are .bash_profile, .bashrc, .bash_login, .profile.

What's the reading sequence between them?

7

Basically, if it's a login shell it sources /etc/profile then .bash_profile. If it's not a login shell, but you're at a terminal, it sources /etc/bash.bashrc then .bashrc.

But it's actually a lot more complicated.

The way I read the man page:

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

It's a login shell any time the shell is run as -bash (note the minus sign) or with the -l option. This usually happens when you log in using the login command (Linux virtual consoles do this), over ssh, or if your terminal emulator has the "login shell" option enabled.

It's an interactive shell any time standard input is a terminal, or bash was started with the -i option. Note that if the shell is also a login shell, bash doesn't check if the shell is interactive. For this reason, .bash_profile usually contains code to source .bashrc, so you can share the same settings between interactive and login shells.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.