Go Premium for a chance to win a PS4. Enter to Win


Redhat Linux 9: .bashrc & .bash_profile

Posted on 2004-04-23
Medium Priority
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
  • 4
  • 3

Accepted Solution

tolgadalkilic earned 100 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 100 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month8 days, 17 hours left to enroll

876 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