How the BASH Shell load its configuration files in Linux

BASH Shell will attempt to load several configuration files during the initialization. There are 2 sets of configuration files, depending it’s a login-shell, or non-login shell.

  • When you login a server on a bare metal monitor, or via SSH, or with # su – [username], you get a login shell.
  • When you start a shell in a terminal in an existing session (gnome-terminal, a shell inside another, or with # su [username]…), you get a non-login shell. A shell script is also executed under a non-login shell.

Below we’ll illustrate the difference by adding some debugging into those configuration files.

Login shell

By default, a login shell will load the following files:

# su - test
this is /etc/profile
this is ~/.bash_profile
this is ~/.bashrc
this is /etc/bashrc

Here note the ~/.bash_profile file, the file by default call ~/.bashrc, and ~/.bashrc call /etc/bashrc.

If for some reason BASH don’t find the file ~/.bash_profile, it will look for ~/.bash_login instead. If again ~/.bash_login cannot be found, BASH will look for ~/.profile instead.

And in the 2 cases, ~/.bashrc, and /etc/bashrc won’t be loaded, unless they were explicitly called in ~/.bash_login or ~/.profile.

The examples when ~/.bash_profile doesn’t exist:

– With presence of ~/.bash_login, no matter ~/.profile exist or not:

$ su - test
Password: 
this is /etc/profile
This is ~/.bash_login

– Only ~/.profile:

# su - test
this is /etc/profile
this is ~/.profile

Non-login Shell

It’s easier, BASH only load the ~/.bashrc, and again, the file call /etc/bashrc.

# su test
this is ~/.bashrc
this is /etc/bashrc
Related Post