When working with Linux, Unix, and Mac OS X, I always forget which bash config file to edit when I want to set my PATH
and other environmental variables for my shell. Should you edit .bash_profile
or .bashrc
in your home directory?
You can put configurations in either file, and you can create either if it doesn’t exist. But why two different files? What is the difference?
So turns out that on Mac OS X Snow Leopard as well as Mac OS X Lion, the file that's loaded is called.profile, not.bashrc. What you want to do is create a file in /.profile and call it.profile (if it doesn't already exists). Put whatever information you needed to. The following lines will download the ROS source code using the rosinstall tool, and bootstrap the installation. The installation downloads all ROS stacks in subdirectories inside the /ros directory, one subdirectory for each stack in the rosinstall file. First install rosinstall using pip (advanced options for pip):. Sudo pip install -U rosinstall vcstools. If you put it in your.bashrc, you’d see it every time you open a new terminal window. Mac OS X — an exception. An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling.bashprofile instead of.bashrc. Other GUI terminal emulators may do the same.
According to the bash man page, .bash_profile
is executed for login shells, while .bashrc
is executed for interactive non-login shells.
What is a login or non-login shell?
When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile
is executed to configure your shell before the initial command prompt.
But, if you’ve already logged into your machine and open a new terminal window (xterm) inside Gnome or KDE, then .bashrc
is executed before the window command prompt. .bashrc
is also run when you start a new bash instance by typing /bin/bash
in a terminal.
Why two different files?
Say, you’d like to print some lengthy diagnostic information about your machine each time you login (load average, memory usage, current users, etc). You only want to see it on login, so you only want to place this in your .bash_profile
. If you put it in your .bashrc
, you’d see it every time you open a new terminal window.
Mac OS X — an exception
An exception to the terminal window guidelines is Mac OS X’s Terminal.app, which runs a login shell by default for each new terminal window, calling .bash_profile
instead of .bashrc
. Other GUI terminal emulators may do the same, but most tend not to.
Recommendation
Most of the time you don’t want to maintain two separate config files for login and non-login shells — when you set a PATH
, you want it to apply to both. You can fix this by sourcing .bashrc
from your .bash_profile
file, then putting PATH
and common settings in .bashrc
.
To do this, add the following lines to
.bash_profile
:Now when you login to your machine from a console .bashrc
will be called.
File TypeBash Non-Interactive Login Shell File
Developer | The GNU Project |
Popularity | |
Category | System Files |
Format | Text |
What is a BASHRC file?
Shell file used by Linux and Mac OS X terminal applications; contains setup instructions for the shell, such as declarations for environment variables and default scripts to run; often used for setting user command prompt preferences as well as paths for common directories and program executables.
BASHRC files are executed automatically when a user creates a new shell that is a non-interactive login shell. In other words, BASHRC files run for every Bash command that executes after the terminal has been created. This can be useful for initializing the same settings for embedded shell scripts.
BASHRC files differ from .BASH_PROFILE files, which execute for interactive login shells (when a user enters login credentials or opens a new terminal).
NOTE: BASHRC files are hidden and do not contain a filename prefix. They always have the filename .bashrc and are located in the user's home directory.