Bash Initialization Files
First, let’s look at which initialization files bash uses. After all, the configuration has to be read in from somewhere. Since bash is to be adapted to personal needs in the following chapters, you need information on where the adjustments should be entered.
If bash is started as a login shell, the commands from the system-wide file /etc/profile are first read and executed. Then it is checked which of the files ~/.bash_profile, ~/.bash_login or ~/.profile is present (in this order). The commands of the first file found are read and executed. In particular, this means that an existing ~/.bash_login or ~/.profile file will not be read (and therefore not executed) if there is a ~/.bash_profile!
When a login shell exits and the ~/.bash_logout file is present and readable, the commands from ~/.bash_logout are executed.
When bash is started as an interactive shell, but not as a login shell, the ~/.bashrc file is read and executed if it exists.
The system-wide initialization file for login shells.
The user-specific initialization file for login shells.
The user-specific initialization file for login shells if ~/.bash_profile does not exist.
The user-specific initialization file for login shells if ~/.bash_profile and ~/.bash_login do not exist.
The user-specific initialization file for interactive shells (not login shell)
User-specific file for cleanup when exiting a login shell.
The /etc/profile and ~/.profile files are also used by other shells (e.g. ksh) and should therefore not contain bash-specific configurations.
In most cases, the adjustments should be user-specific. We will use ~/.bash_profile (login shells) and ~/.bashrc for this, respectively. To avoid having to put the configuration in both files, for login shells and interactive (non-login) shells, we use the following lines in ~/.bash_profile:
# include user-specific startup file .bashrc for interactive non-login shells
[ -f ~/.bashrc ] && . ~/.bashrc
This means that a login shell also reads the ~/.bashrc file and executes the commands it contains. In most cases, we will therefore only make changes in the ~/.bashrc file, unless it is a configuration that should only be valid for a login shell.