Hack 84. Execution Sequence of .bash_* files

What is the sequence in which the following files are executed?

  • /etc/profile
  • ~/.bash_profile
  • ~/.bashrc
  • ~/.bash_login
  • ~/.profile
  • ~/.bash_logout

Execution sequence for interactive login shell

Following pseudo code explains the sequence of execution of these files.

execute /etc/profile
IF ~/.bash_profile exists THEN
    execute ~/.bash_profile
ELSE
    IF ~/.bash_login exist THEN
        execute ~/.bash_login
    ELSE
        IF ~/.profile exist THEN
            execute ~/.profile
        END IF
    END IF
END IF

When you logout of the interactive shell, following is the sequence of execution:

IF ~/.bash_logout exists THEN
    execute ~/.bash_logout
END IF

Please note that /etc/bashrc is executed by ~/.bashrc as shown below:

# cat ~/.bashrc
if [ -f /etc/bashrc ]; then
. /etc/bashrc
Fi

Execution sequence for interactive non-login shell

While launching a non-login interactive shell, following is the sequence of execution:

IF ~/.bashrc exists THEN
    execute ~/.bashrc
END IF

Note: When a non-interactive shell starts up, it looks for ENV environment variable, and executes the file-name value mentioned in the ENV variable.