By default, Terminal starts the shell via /usr/bin/login, which makes the shell a login shell. On every platform (not just Mac OS X) bash does not use .bashrc for login shells (only /etc/profile and the first of .bash_profile, .bash_login, .profile that exists and is readable). This is why "put source ~/.bashrc in your .bash_profile" is standard advice

I usually just put the things that I'd normally put in ~/.bashrc to ~/.profile — has worked so far like a charm.

By default, Terminal starts the shell via /usr/bin/login, which makes the shell a login shell. On every platform (not just Mac OS X) bash does not use .bashrc for login shells (only /etc/profile and the first of .bash_profile, .bash_login, .profile that exists and is readable). This is why “put source ~/.bashrc in your .bash_profile” is standard advice.
–
Chris JohnsenApr 28 '11 at 4:21

3

Re "I usually just put the things that I'd normally put in ~/.bashrc to ~/.profile": For maximum compatibility I recommend you follow Chris Johnsen's advice and place anything that you want to apply to non-login shells in ~/.bashrc and add "source ~/.bashrc" to your ~/.bash_profile to run it for login shells. Otherwise, it won't work for sub-shells, or if Terminal changes to create non-login shells, or if you ever use xterm or another terminal program that create non-login shells by default, or if you might want to use the same setup on another OS.
–
Chris PageAug 14 '11 at 1:35

2

On a related note, on Mac OS X you should consider having ~/.bashrc run /etc/bashrc to pick up global behaviors. In particular, there's code in /etc/bashrc to update the working directory at each prompt, which is what enables Terminal to display the proxy icon, create new terminals at the same directory, and restore the working directory for Resume and Window Groups.
–
Chris PageAug 14 '11 at 1:38

This is my approach. This way I can maintain a .bashrc that works on both systems... and I can put any mac specific stuff or stuff like aliases that are only for work (where I use the mac the most) in my .bash_profile file.
–
Michael DurrantMay 27 '14 at 10:28

+1, this is situation I had. I found that either .bash_login or .bash_profile will prevent .profile from being executed... No idea why bash feels the need to have so many conflicting startup files.
–
RichVelMar 13 '13 at 21:21

Sourcing .profile in .bash_profile is a great thing to do, but that alone wouldn't cause .bashrc to be run by login shells. Your .profile must've already sourced .bashrc, which could cause problems if you ever use a shell other than bash. Most Mac users never will, but if you do for some reason, remember this. :)
–
Terry NMar 19 '14 at 16:56

bashrc is for non-login shells. bash_profile/bash_login/profile/whatever else is for login shells. This is a source of a huge amount of contention, involving what is and isn't a login shell (a screen/tmux session for example, a terminal within a desktop environment, etc.). It's not necessarily the "second shell". It's a matter of shell entry.
–
Jason SalazJun 3 '13 at 20:14

If you ever use a shell that isn't Bash, doing what you describe might cause you problems. .profile applies to all shells and .bashrc might contain language that some other shells don't understand. Instead of .profile, put what you wrote in .bash_profile. (Also, make the first thing .bash_profile does be "source .profile".)
–
Terry NMar 19 '14 at 16:44

For both you want to source ~/.bashrc in your profile that gets loaded, or sourced, when your shell for your terminal starts.

LINUX

In Linux, ~/.profile is automatically source in your shell when it starts. So, if you go to your terminal and type cd ~; ls -A, you will see all of your files and directories in your home directory (/home/usrname/). You should see a file called .profile. This is the file that is automatically "sourced" when you start the terminal.

If you want to add aliases and functions to ~/.bashrc (which is what I do), then you should (inside of ~/.profile add an if statement that checks if ~/.bashrc is a non-empty file and then source it.

To check if your ~/.profile already does this enter nano ~/.profile. This will open it up in a text editor (you can use gedit if you know you have it or even vim if you know how to use it). You may get an empty text file (you shouldn't), but if you do simply proceed. If you do not see a line anywhere that says source ~/.bashrc, then enter the following lines somewhere (put it in a reasonable place like at the end or beginning and not in the middle of an if statement):

if [ -s ~/.bashrc ]; then
source ~/.bashrc;
fi

This checks if ~/.bashrc is a non-empty file (with if [ -s ~/.bashrc ]), and if it is such, it sources it. Pretty simple. Now, you can add any valid alias, function, variable, etc to ~/.bashrc.

MAC OS X

By default, Max Os X sources ~/.bash_profile. To be able to add things to~/.bashrc(which is, again, what I do in Os X, as well), then you follow the same procedure as linux. In the terminal entercd ~; nano .bash_profile. Check for a line that sayssource ~/.bashrcand if it's not there add the if statement above (if [ -s ~/.bashrc ]; then source ~/.bashrc; fi`).

NOTES

When writing an if statement, be sure to leave spaces between basically everything (for example if [ -s ~/.bashrc]; then echo "found"; fi does not work because there is no space beween ~/.bashrc and ] - the interpreter will think this is one word).

If you want to figure out how to check for stuff in an if statement better, go to terminal and enter man \[; this essentially gives you the run-down of an if statement. For example, if [ -f /path/to/file ]; then echo "it's a file"; else echo "not a file"; fi checks if /path/to/file is a file and if it is prints to stdout accordingly. I always reference this as well for easier-on-the-eyes and quick reference: 7.2. File test operators.

I hope this helps. I remember how confused I was when I started all of this stuff (which wasn't too long ago); so, good luck with your future UNIX endeavours!

To keep with the OSX style login .profile and support the normal bash behavior .bashrc you can use a .bashrc file if you switch between regular nix and OSX by providing a symbolic link to your .bashrc file called .bash_profile. Just make sure this file doesn't exist already before trying this, but this is how I do it.

By doing this linking between 2 differents shell initialisation script you are loosing a necessary separation of function. One day or another you will need an initialisation at the session level (.bash_profile) and an initialisation different at each shell level (.bashrc).
–
daniel AzuelosSep 24 '14 at 7:03