[gnu.bash.bug] Init files

jjd@BBN.COM (James J Dempsey) (06/20/89)

Brian Fox writes:
>> ~/.bashrc is sourced in all interactive shells.  Presumably, subshells,
>> those created with "( command )", or "shell-script", have already had
>> ~/.bashrc sourced at a higher level.
>> 
>>      Just what is the order supposed to be (I can read the source, but
>>      I'm looking for the design rationale for init file invocation).
>> 
>> ~/.bash_profile is sourced for each login shell.  ~/.bashrc is sourced
>> for each interactive shell.  If you wish to have the same commands
>> executed for both login and interactive startups, then explicitly
>> "source ~/.bashrc" in your ~/.bash_profile.  This allows you to *not*
>> have the same commadn executed for both login and interactive startups.

I would agree with this except for xterm windows and rsh commands.
When an xterm window gets started up or when you use rsh to execute a
command on a remote host, .bashrc gets executed.  In particular, I
often use rsh to start up xterm on a remote host. This means that I
set things like PATH, PS1, aliases and functions in my .bashrc.
Naturally, I want my PATH and the other things set in my login shell
too, so there is no way I can avoid sourcing my .bashrc from
.bash_profile. 

I think the basic problem is in what Brian says above:  "~/.bashrc is
sourced in all interactive shells."  Perhaps this should be true, but
it isn't.  .bashrc isn't sourced in interactive shells which are login
shells.  It is sourced by rsh commands which aren't interactive.

Having said this, I think there should also be some way of determining
from inside .bashrc whether or not a given session is interactive or
not.  Consider again the example above of starting an xterm from an
rsh.  Both the rsh on the remote host and the xterm will source
.bashrc.  However, the rsh is not connected to a terminal (and is not
interactive) while the xterm is.  This means that the in the xterm
session I want to use stty to set up things like erase and kill
characters and tty driver type (newcrt) while I don't really want to
do this in the rsh session.  Unfortunately, I can't differentiate
between the two inside .bashrc.

I can think of three different classes of commands I would want to
execute on startup.  

     Class          example commands
     --------------------------------
     Always         set PATH
     Login only     set terminal type, login information programs
     Interactive    functions, aliases, stty, most environment vars,shell vars

The "Always" commands should be done by all shells, whether login,
interactive, non-interactive or whatever.  The "Login only" commands
should only be executed by a login shell as .bash_profile is now.  The
"interactive" commands should be executed in any shell where the user
will be typing commands.

There should either be a separate init file for each of these classes
or some way to determine which class of shell you are from inside a
single init file, or some combination.

I apologize for this message getting so long.

		--Jim Dempsey--
		BBN Communications
		jjd@bbn.com (ARPA Internet)
                ..!{decvax, harvard, wjh12, linus}!bbn!jjd

bfox@aurel.caltech.edu (Brian Fox) (06/21/89)

   Date: Tue, 20 Jun 89 10:02:58 -0400
   From: James J Dempsey <jjd@bbn.com>

   Having said this, I think there should also be some way of determining
   from inside .bashrc whether or not a given session is interactive or
   not.

In bash, if PS1 is not set, then the shell is not interactive.

Brian Fox

jjd@BBN.COM (James J Dempsey) (06/21/89)

>> Date: Tue, 20 Jun 89 17:20:37 PDT
>> From: Brian Fox <bfox@aurel.caltech.edu>
>> Subject: Init files (was Questions/bugs.)
>> 
>>    Date: Tue, 20 Jun 89 10:02:58 -0400
>>    From: James J Dempsey <jjd@bbn.com>
>> 
>>    Having said this, I think there should also be some way of determining
>>    from inside .bashrc whether or not a given session is interactive or
>>    not.
>> 
>> In bash, if PS1 is not set, then the shell is not interactive.
>> 
>> Brian Fox

Given, this fact, I can achieve my previously described three types of
init commands in this way:

Contents of .bash_profile:
source .bashrc
# Put all login session only type commands here
eval `tset -s ...`   # an example, set terminal type


Contents of .bashrc
# Put commands which should always be executed here:
PATH=whatever; export PATH     #an example, set the PATH
if [ -z "$PS1" ]
then
#    Put all interactive only commands here
     stty newcrt erase ^?    # an example
fi

However, I still contend that .bashrc should be automatically sourced
by login shells.  It is no problem to source it yourself, but I can't
think of any case where you *wouldn't* want to source .bashrc in
.bash_profile.

In any case, thanks, Brian, for your response.

		--Jim--