Redhat Linux 9: .bashrc & .bash_profile

Posted on 2004-04-23
Last Modified: 2013-12-16
Hi All,

(1) What is the purpose of the .bashrc file?  When and how do I use it?

(2) In .bashrc file, what does the following mean...
        if [ -f /etc/bashrc ]; then
               . /etc/bashrc

(3) What is the purpose of the .bash_profile file?  When and how do I use it?

(4) In .bash_profile, what does the following mean...
         if [ -f ~/.bashrc ]; then
                . ~/.bashrc

(5) If I do env, I get a list of environmental variables  -- where are these variables stored?...I thought that this would be stored in .bashrc or  .bash_profile but they don't seem to have anything related.

(6) In a nutshell, what do the following environment variables mean...
       a) SSH_AGENT_PID
       b) PVM_RSH
       D) SHELL
       E) TERM
       F) GTK_RC_FILES
       G) WINDOWID
       H) QTDIR
       I) USER
       J) LS_COLORS
       K) PVM_ROOT
       N) USERNAME
       O) MAIL
       P) INPUTRC
       R) LANG
       V) SHLVL
       W) XPVM_ROOT
       Y) BASH_ENV
       Z) LOGNAME
      AF) _=
(if you don't have time for this last question, pointing me at a good source will work just as fine).

Thank you very much for your time!

Question by:itorbust
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3

Accepted Solution

tolgadalkilic earned 25 total points
ID: 10902915
>>Hi All,

>>(1) What is the purpose of the .bashrc file?  When and how do I use it?
It is the login script. You can write the commands or set some variables in this file to start when you login

>>(2) In .bashrc file, what does the following mean...
>>        if [ -f /etc/bashrc ]; then
>>               . /etc/bashrc
>>        if
It means if there is a file /etc/bashrc then execute this login script

>>(3) What is the purpose of the .bash_profile file?  When and how do I use it?
Same as question 1

>>(4) In .bash_profile, what does the following mean...
>>         if [ -f ~/.bashrc ]; then
>>                . ~/.bashrc
>>         fi
Like question 2, if there is ~/.bashrc file, then execute it.

These questions are shell dependent and yours are BASH related.
For complete answers you can look at the best guide for bash from the real source at tldp:

Advanced Bash-Scripting Guide
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 25 total points
ID: 10904717
There is a difference between .bashrc and .bash_profile: .bashrc is only read for interactive shells, .bash_profile is read for every bash that is started. This means that you should put commands that only make sense in an interactive environment into .bashrc (e.g. alias definitions), this way these commands don't need to be executed when you start a shell script.

That's the idea... Your example shows that your .bash_profile sources .bashrc, which means that you are always executing all commands.

(5) also check ~/.profile /etc/profile  ~/.bash_login (and there may be more)

(6) Some of these variables are for specific applications. I'll list those that I know about:

       a) SSH_AGENT_PID - used by the ssh-agent (the Secure Shell authentication agent), it holds the process ID of the ssh-agent process
       b) PVM_RSH - "PVM_RSH is a  wrapper program around the UNIX command rsh that allows PVM to  rsh between the nodes of the Sun Fire Cluster only, and carry  forward to all the nodes your current working environment." (From the PVM FAQ)
       D) SHELL - Set to the shell you are currently using (e.g. /bin/bash)
       E) TERM - Set to the terminal type you are currently using (e.g. xterm)
       F) GTK_RC_FILES - probably the path where GTK resource files are searched
       G) WINDOWID - ???
       H) QTDIR - The base directory for the QT files (used by e.g. KDE)
       I) USER - The user name that's currently logged in
       J) LS_COLORS - Used by a version of the ls program that can display colored listings
       K) PVM_ROOT - The base dir of the PVM system
       L) SSH_AUTH_SOCK - Used by ssh-agent
       M) SESSION_MANAGER - ???
       N) USERNAME - Probably set to the current user, but I don't know how this is used.
       O) MAIL - Path of your mail spool file
       P) INPUTRC - ???
       Q) XMODIFIERS - used to configure modifier keys for X11
       R) LANG - used to indicate which language to use for program output
       S) LAMHELPFILE - ???
       T) GDMSESSION - ??? probably used by the Gnome display manager
       U) SSH_ASKPASS - used by ssh to indicate that an external program needs to be run to ask for the password
       V) SHLVL - ???
       W) XPVM_ROOT - ???
       Y) BASH_ENV - holds the name of yet another file that bash will use for configuration data
       Z) LOGNAME - ???
       AA) LESSOPEN - ???
      AB) DISPLAY - Used to X11 to specify which display yuor application should use to show it's windows
      AD) COLORTERM - ???
      AE) XAUTHORITY  - used to authenticate X11 connections
      AF) _= - you probably mean just _ - this is from the bash man page:
              "At  shell  startup,  set  to the
              absolute file name of the shell or
              shell script being executed as passed in the argument list.  Subse­
              quently,  expands  to  the  last  argument to the previous command,
              after expansion.  Also set to the full file name  of  each  command
              executed  and  placed  in the environment exported to that command.
              When checking mail, this parameter holds the name of the mail  file
              currently being checked."
LVL 40

Expert Comment

ID: 10906150
> There is a difference between .bashrc and .bash_profile: .bashrc is only read for interactive
> shells, .bash_profile is read for every bash that is started.

I'm sorry, but that's not correct. .bash_profile (or .bash_login or .profile) is only executed for a login shell. .bashrc is read by every invocation of a shell, interactive or not. So one should only place commands in .bash_profile for those things that need to be set once in a login session and .bash_profile should chain to .bashrc, if it exists. This is documented in the man page for bash in the "INVOCATION" section

Since .bashrc will be executed by both interactive and non-interactive shells it is common to place things needed by both types at the beginning of the script (set up of PATH, LD_LIBRARY_PATH, etc) and place the remainder needed for an interactive shell inside of:

# If running interactively, then we need the following stuff.
if [ "$PS1" ]; then
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10906836
This is from the bash man page:
      "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

My interpretation of this is that .bashrc is only read for interactive shells.
LVL 40

Expert Comment

ID: 10907679
That's exactly what I said. The point was that .bash_profile is only read by a login shell and .bashrc is read by all shell invokations afterwards.
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10911299
I still think there is a difference between "all shell invokations afterwards" and "an interactive shell that is not a login shell".
LVL 40

Expert Comment

ID: 10912318
With respect to .bash_profile and .bashrc the only difference is whether it is an intial login shell. For that shell .bash_profile will be read. For all subsequent shells, interactive or not, only .bashrc is read. An example of the the latter is that starting a new terminal window is an interactive shell and .bashrc will be read. Executing a shell script that begins with #!/bin/bash, etc, is a non-interactive shell that will also read .bashrc.
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10914615
So I guess the man page could use some work... Thanks.

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question