6

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

What's the reading sequence between them?

bahamat
  • 39,666
  • 4
  • 75
  • 104
sam
  • 1,007
  • 1
  • 9
  • 11
  • 3
    See the INVOCATION section of the bash manual. Meanwhile I am sure this is a duplicate of something. – jw013 Jul 25 '12 at 16:44
  • 2
    Try to do some research before asking, man bash contains all the information – Ulrich Dangel Jul 25 '12 at 16:48
  • From man bash, non-interactively means non-login shells? Thank you~ – sam Jul 25 '12 at 16:57
  • An interactive shell is one started without non-option arguments and without the -c option whose standard input and error are both connected to terminals (as determined by isatty(3)), or one started with the -i option. - from man bash. This means if any of these requirements are not fulfilled it is a non-interactive shell, e.g. if you run a script with the #!/bin/bash shebang it is a non interactive shell. – Ulrich Dangel Jul 25 '12 at 17:00
  • Interactive and login are orthogonal concepts. You'll be less confused if you stop trying to align them. All 4 combinations can exist, from interactive login shells to non-interactive non-login shells. – jw013 Jul 25 '12 at 17:05
  • @jw013 Can you clarify? I'm pretty sure login shell (or interactive with --login) means it takes a separate codepath from an interactive shell. – Mikel Jul 25 '12 at 18:29
  • 2
    @jw013 Nearest thing to a duplicate I can find is Difference between .bashrc and .bash_profile on superuser.com. – Mikel Jul 25 '12 at 19:14
  • @Mikel That's what I said right? sam was asking if non-interactive == non-login, or if some other sort of relation between the two traits existed. My previous comment said that there is none, since all 4 possibilities can exist. You can have non-interactive, non-login shells, interactive non-login shells, login non-interactive shells, or interactive login shells. – jw013 Jul 25 '12 at 21:31
  • 2
    @jw013 http://unix.stackexchange.com/questions/3052/alternative-to-bashrc and http://unix.stackexchange.com/questions/40708/what-is-the-difference-between-profile-bashrc-bash-profile-gnomer but neither fully addresses this question – Gilles 'SO- stop being evil' Jul 25 '12 at 23:25

1 Answers1

8

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.

Mikel
  • 57,299
  • 15
  • 134
  • 153