Bash configuration filesEdit
This is my attempt to phrase the "INVOCATION" section of the Bash man page in the clearest possible terms.
Definitions
A "login" shell is:
- a shell whose argument zero starts with
-; or - a shell invoked with the
--loginor-l(equivalent) option
Examples of login shells:
- the shell you get when you:
- open a new terminal window
- open a new tab in a terminal window
- open a new pane in a tmux session
Note: This behavior may vary according to your terminal program; it is true for Mac OS X, but this article claims the behavior is the opposite in some Linux distros.
An "interactive" shell may be:
- a login shell as described above; or
- any other shell whose standard input and output are connected to a real terminal (ie. the kind of shell you get if you type
bashorsh) - a shell started with
-i
A "non-interactive" shell may be:
- a shell started to run a shell script
- a shell started to run a command (eg.
sh -c ls)
Startup behavior
sh:- run as an interactive login shell (unless inhibited with the
--no-profileoption):- runs
/etc/profile - runs
~/.profile, if it exists
- runs
- run as an interactive non-login shell (eg. if you just type
sh)- runs nothing
- run as a non-interactive shell with the
--loginor-loptions:- runs
/etc/profile - runs
~/.profile, if it exists
- runs
- run as an interactive login shell (unless inhibited with the
bash:- run as an interactive login shell (unless inhibited with the
--no-profileoption):- runs
/etc/profile - runs first found, readable file among
~/.bash_profile,~/.bash_loginand~/.profile - runs
~/.bash_logout, if present, on logout
- runs
- run as an interactive non-login shell (eg. if you just type
bash):- runs
~/.bashrc(unless inhibited with the--no-rcoption)
- runs
- run as a non-interactive shell with the
--loginor-koptions:- runs
/etc/profile - runs first found, readable file among
~/.bash_profile,~/.bash_loginand~/.profile - runs
~/.bash_logout, if present, on logout
- runs
- run as a non-interactive shell (eg.
bash -c ls):- runs nothing
- run as an interactive login shell (unless inhibited with the
Consequences
- For most intents and purposes, you can count on
~/.bash_profile(or similar) running for pretty much any shell you open. - It's recommended that you put base config (things like
PATHexports) in your~/.bashrcand fancy stuff (prompt set-up etc) in your~/.bash_profile; you can source you~/.bashrcfrom your~/.bash_profileto that you don't have to duplicate the basic config