bash: Bash Startup Files

1 
1 6.2 Bash Startup Files
1 ======================
1 
1 This section describes how Bash executes its startup files.  If any of
1 the files exist but cannot be read, Bash reports an error.  Tildes are
11 expanded in filenames as described above under Tilde Expansion (⇒
 Tilde Expansion).
1 
1    Interactive shells are described in ⇒Interactive Shells.
1 
1 Invoked as an interactive login shell, or with '--login'
1 ........................................................
1 
1 When Bash is invoked as an interactive login shell, or as a
1 non-interactive shell with the '--login' option, it first reads and
1 executes commands from the file '/etc/profile', if that file exists.
1 After reading that file, it looks for '~/.bash_profile',
1 '~/.bash_login', and '~/.profile', in that order, and reads and executes
1 commands from the first one that exists and is readable.  The
1 '--noprofile' option may be used when the shell is started to inhibit
1 this behavior.
1 
1    When an interactive login shell exits, or a non-interactive login
1 shell executes the 'exit' builtin command, Bash reads and executes
1 commands from the file '~/.bash_logout', if it exists.
1 
1 Invoked as an interactive non-login shell
1 .........................................
1 
1 When an interactive shell that is not a login shell is started, Bash
1 reads and executes commands from '~/.bashrc', if that file exists.  This
1 may be inhibited by using the '--norc' option.  The '--rcfile FILE'
1 option will force Bash to read and execute commands from FILE instead of
1 '~/.bashrc'.
1 
1    So, typically, your '~/.bash_profile' contains the line
1      if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
1 after (or before) any login-specific initializations.
1 
1 Invoked non-interactively
1 .........................
1 
1 When Bash is started non-interactively, to run a shell script, for
1 example, it looks for the variable 'BASH_ENV' in the environment,
1 expands its value if it appears there, and uses the expanded value as
1 the name of a file to read and execute.  Bash behaves as if the
1 following command were executed:
1      if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
1 but the value of the 'PATH' variable is not used to search for the
1 filename.
1 
1    As noted above, if a non-interactive shell is invoked with the
1 '--login' option, Bash attempts to read and execute commands from the
1 login shell startup files.
1 
1 Invoked with name 'sh'
1 ......................
1 
1 If Bash is invoked with the name 'sh', it tries to mimic the startup
1 behavior of historical versions of 'sh' as closely as possible, while
1 conforming to the POSIX standard as well.
1 
1    When invoked as an interactive login shell, or as a non-interactive
1 shell with the '--login' option, it first attempts to read and execute
1 commands from '/etc/profile' and '~/.profile', in that order.  The
1 '--noprofile' option may be used to inhibit this behavior.  When invoked
1 as an interactive shell with the name 'sh', Bash looks for the variable
1 'ENV', expands its value if it is defined, and uses the expanded value
1 as the name of a file to read and execute.  Since a shell invoked as
1 'sh' does not attempt to read and execute commands from any other
1 startup files, the '--rcfile' option has no effect.  A non-interactive
1 shell invoked with the name 'sh' does not attempt to read any other
1 startup files.
1 
1    When invoked as 'sh', Bash enters POSIX mode after the startup files
1 are read.
1 
1 Invoked in POSIX mode
1 .....................
1 
1 When Bash is started in POSIX mode, as with the '--posix' command line
1 option, it follows the POSIX standard for startup files.  In this mode,
1 interactive shells expand the 'ENV' variable and commands are read and
1 executed from the file whose name is the expanded value.  No other
1 startup files are read.
1 
1 Invoked by remote shell daemon
1 ..............................
1 
1 Bash attempts to determine when it is being run with its standard input
1 connected to a network connection, as when executed by the remote shell
1 daemon, usually 'rshd', or the secure shell daemon 'sshd'.  If Bash
1 determines it is being run in this fashion, it reads and executes
1 commands from '~/.bashrc', if that file exists and is readable.  It will
1 not do this if invoked as 'sh'.  The '--norc' option may be used to
1 inhibit this behavior, and the '--rcfile' option may be used to force
1 another file to be read, but neither 'rshd' nor 'sshd' generally invoke
1 the shell with those options or allow them to be specified.
1 
1 Invoked with unequal effective and real UID/GIDs
1 ................................................
1 
1 If Bash is started with the effective user (group) id not equal to the
1 real user (group) id, and the '-p' option is not supplied, no startup
1 files are read, shell functions are not inherited from the environment,
1 the 'SHELLOPTS', 'BASHOPTS', 'CDPATH', and 'GLOBIGNORE' variables, if
1 they appear in the environment, are ignored, and the effective user id
1 is set to the real user id.  If the '-p' option is supplied at
1 invocation, the startup behavior is the same, but the effective user id
1 is not reset.
1