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.


Except when being invoked with specific option ( --noprofile, --norc, --rcfile, --init-file), a bash session defines their loading startup scripts and their order based on 2 properties: interactive shell? and login shell?.

Interactive Shell is a shell whose input, error, output are connected to terminals (a tty). Typically, interactive shells reads/writes from/to a terminal. -i option is used to explicitly set interactive mode. [[ -z "$PS1" ]] is a handy condition to check if a shell is in interactive mode. Or, this can be checked via whether $- includes i character.

Login Shell: when being invoked with --login ( -l) option, the shell becomes a login shell.

Startup files

If a shell is a login shell, unless being invoked with --noprofile, startup scripts execution order (if they exist) is: /etc/profile, ~/.bash_profile, ~/.bash_login, ~/.profile.
When a login shell exits, it executes ~/.bash_logout, if it exists.

If a shell is invoked in interactive mode (invoked by terminal, or with -i option), unless --norc option is specified, ~/.bashrc is read and executed if it exists. This file can be explicitly specified with --rcfile file option.

If a shell is invoked in non-interactive mode (running a command), BASH_ENV environment is expanded. If the is a value which matches an existing readable file, that file is executed.

When a graphical session (gnome, unity, kdm, kdm, ..) starts, their behaviors are highly depended on the graphical programs and mostly these behaviors are configurable. There is no consistent rule for all graphical programs. Even though, with a 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 default ~/.profile which sources ~/.bashrc. You should also check: /etc/bashrc, /etc/bash.bashrc, /etc/profile. It is common in many distribution that all scripts in /etc/profile.d/ directory are loaded.