The process of init a Linux user session
┌──────────────────────────┐
│ Interactive? │──────────▶ No
└──────────────────────────┘
│ │
▼ │
│
Yes ▼
┌────────────────────┐
│ │ --login Flag? │───────▶ No
▼ └────────────────────┘
┌──────────────────────────┐ │ │
│ Login Shell? │──┐ ┌──────┘ ▼
└──────────────────────────┘ │ ▼ .───────────────.
│ │ ( $BASH_ENV )
▼ └─▶ Yes ───┐ `───────────────'
│
No ▼
┌────────────────────┐
│ │ --noprofile Flag? │─────────▶ No
▼ └────────────────────┘
┌──────────────────────────┐ │ │
│ --rcfile <file> Flag? │────▶ Yes ─┐ │ │
└──────────────────────────┘ │ └──────────┐ │
│ │ │ │
▼ ▼ ▼ │
.───────────────. │
No ( <file> ) No │
`───────────────' │
│ │ │
▼ │ ▼
┌───────────────────────────┐ │ .───────────────.
│ --norc Flag? │───▶ Yes ──┬───────────┘ ( /etc/profile )
└───────────────────────────┘ │ `───────────────'
│ │ │
▼ ▼ │
╔════════════════════╗ ▼
No ║ No File Loaded! ║ /────────────────────────\\
╚════════════════════╝ │ First Of: │
│ │ - $HOME/.bash_profile │
▼ │ - $HOME/.bash_login │
.───────────────────────. │ - $HOME/.profile │
( /etc/bash/bash.rc ) \\────────────────────────/
`───────────────────────' │
│
│ .───────────────────. │
└──────────────▶( $HOME/.bashrc )◀ ─ ─ ─ ─ ─ ─ May Include
`───────────────────'
Invoked as an interactive login shell, or with --login
When Bash is invoked as an interactive login shell, or as a non-interactive shell
with the --login option, it first reads and executes commands from the file /etc/profile,
if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login,
and ~/.profile, in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the shell is started to
inhibit this behavior.
When a login shell exits, Bash reads and executes commands from the file ~/.bash_logout,
if it exists.
Invoked as an interactive non-login shell
When an interactive shell that is not a login shell is started, Bash reads and executes
commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc
option. The --rcfile file option will force Bash to read and execute commands from file
instead of ~/.bashrc.
By default, Terminal starts the shell via /usr/bin/login, which makes the shell a
login shell. On every platform (not just Mac OS X) bash does not use .bashrc
for login shells (only /etc/profile and the first of .bash_profile, .bash_login,
.profile that exists and is readable). This is why “put source ~/.bashrc in
your .bash_profile” is standard advice.
So typically (or sooner or later), what you will encounter are non-login shells. So this
case is what you should write your config files for. This means putting most of your stuff
in ~/.bashrc, having exactly one of ~/.bash_profile, ~/.bash_login, and ~/.profile,
and sourcing ~/.bashrc from it.
On startup, bash executes any script labeled A in the table below, followed by
the first script B it finds. On exit, it executes any script labeled C.
| Interactive login | Interactive non-login | Non-interactive | Remote shell | |
|---|---|---|---|---|
/etc/profile |
A | |||
/etc/bash.bashrc |
A | |||
~/.bashrc |
B | A | ||
~/.bash_profile |
B1 | |||
~/.bash_login |
B2 | |||
~/.profile |
B3 | |||
BASH_ENV |
A | |||
~/.bash_logout |
C |
Author is lost
The author of the scheme is lost :(
If you know who the author is, please let me know.