Solved

Redhat Linux 9: .bashrc & .bash_profile

Posted on 2004-04-23
8
2,122 Views
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
        if

(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
         fi

(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
       C) DESKTOP_STARTUP_PID
       D) SHELL
       E) TERM
       F) GTK_RC_FILES
       G) WINDOWID
       H) QTDIR
       I) USER
       J) LS_COLORS
       K) PVM_ROOT
       L) SSH_AUTH_SOCK
       M) SESSION_MANAGER
       N) USERNAME
       O) MAIL
       P) INPUTRC
       Q) XMODIFIERS
       R) LANG
       S) LAMHELPFILE
       T) GDMSESSION
       U) SSH_ASKPASS
       V) SHLVL
       W) XPVM_ROOT
       X) GNOME_DESKTOP_SESSION_ID
       Y) BASH_ENV
       Z) LOGNAME
       AA) LESSOPEN
      AB) DISPLAY
      AC) G_BROKEN_FILENAMES
      AD) COLORTERM
      AE) XAUTHORITY
      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!


0
Comment
Question by:itorbust
  • 4
  • 3
8 Comments
 
LVL 3

Accepted Solution

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

>>(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
http://www.tldp.org/LDP/abs/html/index.html
0
 
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)
       C) DESKTOP_STARTUP_PID - ???
       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 - ???
       X) GNOME_DESKTOP_SESSION_ID - ???
       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
      AC) G_BROKEN_FILENAMES - ????
      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."
0
 
LVL 40

Expert Comment

by:jlevie
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
...
fi
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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
       ~/.bashrc."

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

Expert Comment

by:jlevie
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.
0
 
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".
0
 
LVL 40

Expert Comment

by:jlevie
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.
0
 
LVL 44

Expert Comment

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

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

821 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