Bash: about .bashrc, .bash_profile, .profile, /etc/profile, etc/bash.bashrc and others

3 October, 2005 - 17:08
Categories:

Ever wondered what's the difference between ~/.bashrc, ~/.bash_profile, ~/.profile, /etc/profile, /etc/bash.bashrc (and maybe others) and what their purposes are? I do.

Some interesting excerpts from the bash manpage:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
...
When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.

As far as I understand, a login shell means a session where you log in to the system and directly end up in Bash, like a remote ssh session or logging in through a non-graphical text terminal. A non-login shell is then the type of shells you open after logging in: typically in a graphical session when you open a new terminal window.

How I think things are supposed to work (for a typical setup):

  • .profile is for things that are not specifically related to Bash, like environment variables PATH and friends, and should be available anytime. For example, .profile should also be loaded when starting a graphical desktop session.
  • .bashrc is for the configuring the interactive Bash usage, like Bash aliases, setting your favorite editor, setting the Bash prompt, etc.
  • .bash_profile is for making sure that both the things in .profile and .bashrc are loaded for login shells. For example, .bash_profile could be something simple like
    . ~/.profile
    . ~/.bashrc

    As stated in the man page excerpt above, if you would omit .bash_profile, only .profile would be loaded.

You might also be interested in the page I put together with the most important stuff from my .bashrc, .profile and other files.

13 March, 2011 - 18:34

/etc/bashrc or bash.barc versus ~/.bashrc

Anonymous (not verified)

It appears that /etc/bashrc (or /etc/bash.bashrc) is only called/run if it has an entry in your home ~/.bashrc to run it. Otherwise only ~/.bashrc is run. This is the behavior on my Linux computer. There appears to be incorrect information about /etc/bashrc running regardless in circulation, and if there is a way to make that happen, I would like to hear it.

15 February, 2011 - 00:47

My ~/.profile is not sourced

Marnix A. van Ammers (not verified)

Strangely, in my PowerPC system running Ubuntu 10.10 with bash 4.1.5(1)-release, my ~/.profile is not sourced when I login, even though I don't have a ~/.bash_profile nor a ~/.bash_login . If I create a ~/.bash_login, that is sourced. If I have both a ~/.bash_login and a ~/.bash_profile, then, as expected, only ~/.bash_profile is sourced. In no case is my ~/.profile sourced.

This is odd and contrary to the bash manual.

15 February, 2011 - 01:11

RE: My ~/.profile is not sourced

Marnix A. van Ammers (not verified)

Dang. Now it seems to be working. I'm not sure where or how. Ignore my earlier message about my .profile not being sourced.

13 January, 2011 - 04:44

Very helpful, thanks.

Anonymous (not verified)

Very helpful, thanks.

14 December, 2010 - 20:35

Gotcha

Alex Baeza (not verified)

Be careful. The .profile is not run if .bash_profile exists.

the run order is:
1. etc/profile
2. The next file is one of the following, if one is found none of the others are run:
a. ~/.bash_profile
b. ~/.bash_login
c. ~/.profile

18 November, 2010 - 02:09

well explained

vyncynt (not verified)

thanks.

7 November, 2010 - 15:21

I was blind, but now I can see

Anonymous

Thanks for the article. It clears up the whole mystery of just what on earth is going on.

4 October, 2010 - 23:04

Thanks:)) Great help

Anonymous (not verified)

Thanks:))

Great help

17 September, 2010 - 21:05

Thank you.

Anonymous (not verified)

Thank you.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options