login shell VS interactive shell

http://groups.google.com/group/linux.debian.user/browse_thread/thread/2b71ecfc45789958/7bff24e3bae74b36?lnk=raot

At login time the /bin/login program launches the shell listed in your /etc/passwd file but it starts it in a special way so as to communicate to it that it is the very first shell for this user.  Due to limitations at the time this is done by putting a '-' at the front of the shell's name in the process listing.  For example if you log into a computer using ssh and look at your $0 you will find that the shell process name starts with a dash.

 mydesktop$ ssh otherhost     <--- bash reads /etc/profile then

                                    reads .bash_profile (or .profile)
  otherhost$ echo $0
  -bash                        <--- name indicates it is a login shell

Shells look at their own name and if they see that it starts with a dash then they know that they are a login shell.  As a login shell the original Bourne shell from years ago knew it should read the user's $HOME/.profile file.  But while ksh, bash, zsh, others are similar to Bourne shell they all expand upon it.  They all read /etc/profile since that is common to all.  Then bash checks to see if there is a .bash_profile file and if so uses that as an exact match.  If not then bash reads the standard shell .profile file which should contain only generic commands and could be shared among many shells that all are Bourne-shell-like.

Note that a login shell such as bash reads .bash_profile (or .profile) but does *not* read the .bashrc file.  That is why the .bashrc file is almost always sourced explicitly from the .bash_profile (or .profile) file.  If you want it then you need to specify it.  This is so that in the special cases when it is not wanted that there is a way to avoid
it.  Most of the time you want it. That is all about login shells.  But non-login interactive bash shells read the $HOME/.bashrc file.  At the command prompt if you were to
launch another shell it would simply be an interactive shell and would only read the .bashrc file.

  $ bash                  <--- bash reads .bashrc
  $ echo $0
  bash

They do not need to read the .profile again since any exported environment variables will be inherited automatically.  This is why PATH is set in the .profile and then inherited automatically into other shells.  However aliases are not inherited.  Therefore aliases are typically placed in the .bashrc file.

Non-interactive bash shells do not source any files.  However that can be forced.

As you can see the difference between an interactive login shell and an interactive non-login shell is simply one of organization. 


When Bash starts executes the commands in a variety of different scripts. When started as an interactive login shell: Bash reads and executes the /etc/profile (if it exists). After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and reads and executes the first one (that exists and is readable). When
a login shell exits: Bash reads and executes ~/.bash_logout (if it exists). When started as an interactive shell (but not a login shell): Bash reads and executes ~/.bashrc (if it exists).

你可能感兴趣的:(login shell VS interactive shell)