Classic Shell Scripting - Arnold Robbins [216]
login: bones
Password: Echo suppressed to hide password
DEBUG: This is /etc/profile
DEBUG: This is /home/bones/.bash_profile
$ exit
Terminate the session
logout
DEBUG: This is /home/bones/.bash_logout
An interactive session invokes only a single file:
$ bash
Start an interactive session
DEBUG: This is /home/bones/.bashrc
$ exit
Terminate the session
exit
A noninteractive session normally does not invoke any file:
$ echo pwd | bash
Run a command under bash
/home/bones
However, it will if the BASH_ENV value points to a startup file:
$ echo pwd | BASH_ENV=$HOME/.bashenv bash
Run a command under bash
DEBUG: This is /home/bones/.bashenv
/home/bones
Z-Shell Startup and Termination
The Z-shell, zsh, can masquerade as either the Bourne shell or the Korn shell. When invoked under the names sh or ksh, or any name that begins with the letters s or k, optionally preceded by a single r (for restricted), it has the same startup behavior as those shells, and the rest of this section does not apply. (When mimicking ksh, it follows the ksh88 behavior of always processing the $ENV file.)
The Z-shell has the most complex, and most flexible, customization procedure. Every Z-shell startup, whether for a login shell, an interactive shell, or a noninteractive shell, begins by trying to read two initialization files, like this:
test -r /etc/zshenv && . /etc/zshenv Read system-wide script
if test -n "$ZDOTDIR" && test -r $ZDOTDIR/.zshenv ; then
. $ZDOTDIR/.zshenv Read this file
elif test -r $HOME/.zshenv ; then
. $HOME/.zshenv Or else this file
fi
The ZDOTDIR variable provides a way for system management to prevent zsh from automatically reading startup files in user home directories, and instead, to force reading them from somewhere else that is under management control. If that variable is needed, then it would be set in /etc/zshenv, so you can look there to see what your system does.
Assuming that ZDOTDIR is not set, the best place to put personal customizations that you want to be in effect for every Z-shell session is in the file $HOME/.zshenv.
If the shell is a login shell, it next does the equivalent of these commands to read two startup profiles:
test -r /etc/zprofile && . /etc/zprofile Read system-wide script
if test -n "$ZDOTDIR" && test -r $ZDOTDIR/.zprofile ; then
. $ZDOTDIR/.zprofile Read this file
elif test -r $HOME/.zprofile ; then
. $HOME/.zprofile Or else this file
fi
If the shell is a login shell or an interactive shell, it then tries to read two startup scripts like this:
test -r /etc/zshrc && . /etc/zshrc Read system-wide script
if test -n "$ZDOTDIR" && test -r $ZDOTDIR/.zshrc ; then
. $ZDOTDIR/.zshrc Read this file
elif test -r $HOME/.zshrc ; then
. $HOME/.zshrc Or else this file
fi
Finally, if the shell is a login shell, it tries to read two login scripts like this:
test -r /etc/zlogin && . /etc/zlogin Read system-wide script
if test -n "$ZDOTDIR" && test -r $ZDOTDIR/.zlogin ; then
. $ZDOTDIR/.zlogin Read this file
elif test -r $HOME/.zlogin ; then
. $HOME/.zlogin Or else this file
fi
When zsh exits, if it is a login shell, and it is not terminating due to exec'ing another process, it finishes by reading two termination scripts: a user one and a system one, in that order:
if test -n "$ZDOTDIR" && test -r $ZDOTDIR/.zlogout ; then Read this file
. $ZDOTDIR/.zlogout
elif test -r $HOME/.zlogout ; then Or else this file
. $HOME/.zlogout
fi
test -r /etc/zlogout && . /etc/zlogout Read system-wide script
The Z-shell initialization and termination procedures are complex. To make it easier to see what is happening, we instrumented each of the files with an echo command, and we left ZDOTDIR unset so that files are looked for only in /etc and $HOME. A login session then looks likes this:
$ login
Start a new login session
login: zabriski
Password: Echo suppressed to hide password
DEBUG: This is /etc/zshenv
DEBUG: This is /home/zabriski/.zshenv
DEBUG: This is /etc/zprofile
DEBUG: This is /home/zabriski/.zprofile
DEBUG: This