William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

Linux: .bash_profile vs. .bash_rc

Here I summary 2 basic Linux files which initializes user command-line environment. They are .bash_profile and .bash_rc (Suppose default shell is /bin/bash).

.bash_profile and .bash_rc are hidden in each user’s $HOME/ directory. To check it:

$ cd $HOME
$ ls -la .bash_profile .bash_rc

with $HOME/.exrc and other hidden files in $HOME dir (start with .), they build the user’s basic command-line environment: path, alias, term attributes, initial scripts etc. e.g., I use ‘set -o vi’ (in .bashrc) or ‘export EDITOR=vi’ (in .bash_profile) for quick retrieve, and a lot of ‘alias ‘ to simplify operation.

To distinguish the 2 files is easy:

  • .bash_profile is executed automatically when user login,
    When we login (type username and password) via console, or via ssh, .bash_profile is executed to configure command-line env, setup path before the initial command prompt.
  • After login, when running scripts or opening new term, .bashrc is executed automatically.
    If we’ve already logged into Linux and open a new terminal window (xterm) inside Gnome or KDE or via term like vt220, then .bashrc is executed before the window command prompt.

    Or, when a script with shebang at the first line of the script, it hides to call .bashrc to initialize a new env for script to running.

    #! /bin/bash

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.

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:

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

One response to “Linux: .bash_profile vs. .bash_rc

  1. Armando Kramer 02/22/2011 at 10:59 pm

    It has been modified from Debians default .bash profile script so that it will attempt to mount the users Samba share to their home directory if it is not already mounted. This file is located at home sys osc homerun .bash profile… . .bash profile executed by bash 1 for login shells.. see usr share doc bash examples startup-files for examples.. the files are located in the bash-doc package… the default umask is set in etc login.defs. umask 022.. include .bashrc if it exists.if -f .bashrc then.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s