≡ Menu

Execution sequence for .bash_profile, .bashrc, .bash_login, .profile and .bash_logout

This article will explain 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 execute the file-name value mentioned in the ENV variable.

Test the sequence of execution

One of the ways to test the sequence of execution is by adding different PS1 values to these files and re-login to the shell and see which PS1 value got picked up by the Linux prompt. Also, earlier we discussed about how to use PS1 to make your Linux prompt both functional and stylish.

1. /etc/profile gets executed. Add following PS1 line to /etc/profile and re-login to make sure the Linux prompt changes to the PS1 value set inside the /etc/profile.

# grep PS1 /etc/profile
PS1="/etc/profile> "

[Note: re-login to see the prompt change as shown below]
Last login: Sat Sep 27 16:43:57 2008 from 192.168.1.2
/etc/profile>

Please make sure ~/.bash_profile doesn’t have any PS1 for the above to work properly.

2. ~/.bash_profile gets executed: Add following PS1 to ~/.bash_profile, ~/.bash_login, ~/.profile and ~/.bashrc. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.bash_profile as shown below.

/etc/profile> grep PS1 ~/.bash_profile
export PS1="~/.bash_profile> "

/etc/profile> grep PS1 ~/.bash_login
export PS1="~/.bash_login> "

/etc/profile> grep PS1 ~/.profile
export PS1="~/.profile> "

/etc/profile> grep PS1 ~/.bashrc
export PS1="~/.bashrc> "

[Note: Upon re-login, it executed /etc/profile first and ~/.bash_profile next.
So, it took the PS1 from ~/.bash_profile as shown below.
It also did not execute ~/.bash_login, as ~/.bash_profile exists]
Last login: Sat Sep 27 16:48:11 2008 from 192.168.1.2
~/.bash_profile>

3. ~/.bash_login gets executed. Rename the .bash_profile to something else. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.bash_login as shown below.

~/.bash_profile> mv .bash_profile bash_profile_not_used

[Note: Upon re-login, it executed /etc/profile first.
Since it cannot find ~/.bash_profile, it executed ~/.bash_login]
Last login: Sat Sep 27 16:50:55 2008 from 192.168.1.2
~/bash_login>

4. ~/.profile gets executed. Rename the .bash_login to something else. Re-login to make sure the Linux prompt changes to the PS1 value set inside the  ~/.profile as shown below.

~/.bash_login> mv .bash_login bash_login_not_used

[Note: Upon re-login, it executed /etc/profile first.
Since it cannot find ~/.bash_profile and ~/.bash_login, it executed ~/.profile]
Last login: Sat Sep 27 16:56:36 2008 from 192.168.1.2
~/.profile>

5. ~/.bashrc gets executed for non-login shell testing. Executing bash” at the command prompt will give another non-login shell, which will invoke .bashrc as shown below.

~/.profile> bash

[Note: This displays PS1 from .bashrc as shown below.]
~/.bashrc> exit
exit

[Note: After exiting from non-login shell, we are back to login shell]
~/.profile>


If you like this article, please bookmark it on del.icio.us and Stumble it.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 10 comments… add one }

  • Mitko October 7, 2008, 1:01 am

    Great article! Thanks.

  • Anonymous November 26, 2008, 1:24 am

    great!beautiful article

  • Anonymous December 4, 2008, 7:14 am

    Thank you very much. I’m a Linux/Unix beginner. My English is also very poor. I consider your site very, very useful. Thanks.

  • Anonymous October 30, 2009, 11:40 am

    Very good article. It is very useful. Thanks.

  • Nudge February 2, 2010, 12:33 pm

    Could that differ from distro to distro? What distro has this been tested on?
    Thanks.

  • Satya Prakash June 8, 2011, 1:28 am

    Many of the article written here about Linux is well written and very informative. This is also very well written.
    Thanks

  • Muthu August 17, 2012, 4:16 pm

    Good

  • Anonymous September 13, 2012, 4:27 am

    You missed ~/.bashrc at “Execution sequence for interactive login shell” section.

  • Sylar December 1, 2013, 4:40 am

    Many thanks for your excellent article. but what’s the purpose for Linux to set so many “profile file” files?

  • Cesar March 27, 2014, 8:50 am

    Ive searching on the Web and saw this beautiful article. Even its from 2008, is great and very, very useful nowadays.
    Thank you for posting that great explanation!
    Cesar

Leave a Comment