What's the difference between /etc/bash.bashrc and ~/.bashrc? Which one should I use?

20

5

When should I use each of the two .bashrc files to set my aliases, prompt, etc?

cfischer

Posted 2009-10-01T14:01:06.967

Reputation: 7 733

Answers

34

/etc/bash.bashrc applies to all users

~/.bashrc only applies to the user in which home folder it is.

Loïc Wolff

Posted 2009-10-01T14:01:06.967

Reputation: 1 947

1Under Ubuntu, this file, as commented at the beginning, has to be "sourced" from the /etc/profile file. I added an alias command at the end of the /etc/bash.bashrc, and appended the command "source /etc/bash.bashrc" at the end of the /etc/profile file. Works like a charm. – jfmessier – 2010-02-06T02:13:42.423

3And implied in dex's answer is... Use your local ~/.bashrc in all cases except where you want to enforce your will on everyone who uses that machine. – dacracot – 2009-10-01T14:21:47.243

Strictly speaking, you're not enforcing anything in /etc/bash.bashrc because users can always change it in their own ~/.bashrc – Kim – 2009-10-01T14:57:06.063

...except for when someone decides to make all variables readonly in /etc/bash.bashrc :\ – user1686 – 2009-10-01T18:20:35.837

2

According to the GNU Bash Documentation:

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.

Invoked as an interactive non-login shell When an interactive shell that is not a login shell is started, Bash reads and executes commands from ~/.bashrc, if that file exists. 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 ~/.bashrc.

So, typically, your ~/.bash_profile contains the line

if [ -f ~/.bashrc ]; then . ~/.bashrc; fi

after (or before) any login-specific initializations.

Felipe Alvarez

Posted 2009-10-01T14:01:06.967

Reputation: 1 666

1

Not really. Look at this file http://git.savannah.gnu.org/cgit/bash.git/tree/shell.c, in run_startup_files() function where SYS_BASHRC is used, defined in http://git.savannah.gnu.org/cgit/bash.git/tree/config-top.h

– mloskot – 2012-06-10T00:52:31.497

1

For your personal preferences and personal scripts or bash functions you should use .bashrc ( aliases, Added functions to bash ... )

The moment that you want to share something with all users ( or most of users ) or for things of general use ( Path for shared executables , path for documentation ...) put it in /etc/bash.bashrc

I said most of users because even let's say you specify a script greetings.sh which prints "Hello world!" for all users, but user Pepe want to use instead the script greetings.sh which prints "Hola el mundo!". He can modify his path in his .bashrc to point to his script instead of yours. In other word the user can always customize his session in .bashrc to what ever he wants.

Debugger

Posted 2009-10-01T14:01:06.967

Reputation: 261