This question comes up quite often:
"What do all those config files do? When are they run?"
Where are my config files located?
Good question. Usually, zsh's global config files are located in
/etc (eg. /etc/zshrc) and your personal configs are located in your home directory (eg. /home/ft/.zshrc).
But zsh is very flexible, so the location of the global config files (via compile time options), as well as the personal config files (via
$ZDOTDIR), may be changed.
$ZDOTDIR is easy (and left as an exercise for the reader). Note, if
$ZDOTDIR isn't set,
$HOME is the location for per-user-config files.
Checking for the global location takes a little trick:
zsh% strings =zsh | grep zshrc /etc/zsh/zshrc .zshrc
See? That's the output on a debian System, where the global configs are put in
When are they read?
There are several types of config files for zsh. For each type there are two files: one global and one per-user file. The global file is read before the per-user one.
If you do not know what an interactive shell or a login shell is, please read chapter 2 of the users guide (at least 2.1). I know people are impatient, so a here's quick way of testing, if you are in a login shell:
if [[ -o login ]] ; then echo login shell else echo _no_ login shell fi
Yes, you can test for interactive shells in the same way (but isn't that rather obvious?).
Here's a quick listing (first name is the global config file; the 2nd [the one with the dot] is corresponding the per-user config file). Note, that this listing is in chronological order.
zshenv / /
- zshenv is the 1st file zsh reads; it's read for every shell, even if started with -f (setopt NO_RCS)
- .zshenv is the same, except that it's not read if zsh is started with -f
zprofile / /
- read after zshenv, if the shell is a login shell
zshrc / /
- read after zprofile, if the shell is an interactive shell
zlogin / /
- read after zshrc, if the shell is a login shell
- note, that by zprofile and zlogin, you are able to run commands for login shells before and after zshrc
These are similar to Startup Files, so I'll add them here.
Shutdown Files are run, when a login shell exits. The available files are:
zlogout (Note, that for Shutdown files, the order is different to Startup files: first the per-user file is read, then the global one).
What do I put in .zshenv
As little as possible.
From it's name you would suppose that it is meant for environment variables. Environment variables are inherited and it is best to set them on the initial login and have new instances of zsh keep the inherited values. Resetting things like $PATH can break things that have intentionally tweaked it. One common trick is to only set variables in .zshenv if $SHLVL is 1, for example:
if [[ $SHLVL = 1 ]]; then path+=/usr/local/nmh/bin typeset -U path fi
This can help ensure that you have a meaningful path when running commands through something like ssh or su without doing a full login.