Hack 84. Execution Sequence of .bash_* files

by Ramesh

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.

  1. execute /etc/profile
  2. IF ~/.bash_profile exists THEN
  3. execute ~/.bash_profile
  4. ELSE
  5. IF ~/.bash_login exist THEN
  6. execute ~/.bash_login
  7. ELSE
  8. IF ~/.profile exist THEN
  9. execute ~/.profile
  10. END IF
  11. END IF
  12. END IF

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

  1. IF ~/.bash_logout exists THEN
  2. execute ~/.bash_logout
  3. END IF

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

  1. # cat ~/.bashrc
  2. if [ -f /etc/bashrc ]; then
  3. . /etc/bashrc
  4. Fi

Execution sequence for interactive non-login shell

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

  1. IF ~/.bashrc exists THEN
  2. execute ~/.bashrc
  3. 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.