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

Solution 1:

/etc/bash.bashrc applies to all users

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

Solution 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.

Solution 3:

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.