Bash – Where to find the .bashrc file on Mac OS X Snow Leopard and Lion

.bash-profilebashosx lionosx-snow-leopardterminal

I want to install rvm on my Snow Leopard machine.

It says I need to add a line to my .bashrc file (I'm using bash) but where is my .bashrc file?

Best Answer

Regarding the problem with .bashrc above:

On most systems, ~/.bashrc is only used when starting an interactive non-login shell. However, when you start up a new shell it is often an interactive login shell. Since this is a login shell, the .bashrc is ignored. To keep the environment consistent between non-login and login shells, you must source the .bashrc from your .profile or your .bash_profile.

See the Bash Reference Manual, section 6.2 Bash Startup Files

Invoked as an interactive login shell, or with --login

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.

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.

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

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

after (or before) any login-specific initializations.

On my Mac (Running Leopard), there was no line to source ~/.bashrc. I had to add this functionality on my own.

On some systems and other OSes, .bashrc is sourced from the global /etc/profile or /etc/bash_profile , or is done using the template files from /etc/skel.

To be honest the distinction between .bashrc and .bash_profile is not well understood by the community. When many developers say "Add this to your .bashrc", what they really mean is "Add this to your .bash_profile". They want the functionality to be added to your login shell (which is .bash_profile), not to your non-login shell. In reality, it doesn't usually matter and placing configuration in .bashrc is acceptable.

Related Question