Sometimes, you are confusing why some startup scripts are not executed. Sometimes, they are executed multiple times.
This post will reveal the reason for these phenomenons.
The basic definitions
Except when being invoked with one or more of these options (
--init-file), a bash session defines the startup scripts to be loaded and their orders based on two boolean properties: interactive/non-interactive and login/non-login.
Interactive Shell is a shell whose input, error, output are connected to terminals (tty).
Typically, an interactive shell reads from/writes to a terminal. The
-i option is used to set interactive mode explicitly. On the other hand,
[[ -z "$PS1" ]] is a handy condition to check if a shell is in interactive mode. Likewise, this can be checked via whether
Startup scripts and their execution orders
If a shell is a login shell, unless being invoked with
--noprofile, the startup scripts' execution orders (if they exist) are:
In addition, when a login shell exits, it executes
~/.bash_logout, if the script exists.
If a shell is invoked in interactive mode (invoked by a terminal, or invoked with the
-i option), unless the
--norc option is specified,
~/.bashrc is read and executed if the script exists. This script can be changed with the
--rcfile file option.
If a shell is invoked in non-interactive mode (running a command), the
BASH_ENV environment variable is expanded. If the is a value that matches an existing readable file, that file is executed.
When a graphical session (gnome, unity, kdm, kdm, ..) starts, their behaviors are highly dependent on the graphical programs, which are mostly configurable. There is no consistent rule for all graphical programs. Even though, with the same program, they vary from version to version. Refer to this superuser answer. Typically,
~/.profile is read, but you should not rely on this behavior.
Some Linux distributions have a default
~/.profile which sources
~/.bashrc. Besides, you should also check:
/etc/profile. Also, It is common in many distribution that all scripts in
/etc/profile.d/ directory are loaded.