Solved

Redhat Linux 9: .bashrc & .bash_profile

Posted on 2004-04-23
8
2,094 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now