[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2173
  • Last Modified:

Redhat Linux 9: .bashrc & .bash_profile

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!

  • 4
  • 3
2 Solutions
>>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
Karl Heinz KremerCommented:
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."
> 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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Karl Heinz KremerCommented:
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.
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.
Karl Heinz KremerCommented:
I still think there is a difference between "all shell invokations afterwards" and "an interactive shell that is not a login shell".
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.
Karl Heinz KremerCommented:
So I guess the man page could use some work... Thanks.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now