DEV Community

Cover image for zshLoadOrder
alephalpha0
alephalpha0

Posted on

zshLoadOrder

ZSH shell startup/shutdown files.

This is really just for my own reference, as ZSH loads its config in its own order and I can never seem to remember it. The global files (which shouldn't really be tampered with by users) will be mentioned and put into larger font than the user files. However, I won't be expounding upon them, as that is outside the purpose of this writing.

/etc/zshenv above all else. The alpha, the omega, the genesis.

.zshenv (req to be in $HOME)

Here there be environmental variables.
Place here anything I'm going to export so that it can be referenced later.
Good candidates include XDG_* and changes to program defaults.
Set up your $PATH & $PAGER & etc etc etc.

Quality of life: export ZDOTDIR="$XDG_CONFIG_HOME/zsh"

/etc/zprofile ONLY if the shell is a login shell.

.zprofile ($ZDOTDIR/.zprofile) ONLY if shell is login.

Any commands you want happening on every login.
Start daemons, set a new MOTD, start a SSH tunnel.
The possibilities are endless.

/etc/zshrc ONLY if the shell is interactive.

.zshrc ($ZDOTDIR/.zshrc) ONLY if the shell is interactive.

Used for shell options and various commands.
There should be a loooong list of setopt and unsetopt going on here.
Shell modules and the ZSH prompt go here, as well as history config.

Source your scripts and set your aliases.

/etc/zlogin ONLY if shell is login.

.zlogin ($ZDOTDIR/.zlogin) ONLY if shell is login.

Almost same purpose as .zprofile but you know, not really different. Use only one or the other if you're flying by the book.

.zlogout ($ZDOTDIR/.zlogout) ONLY if shell is login.

Can be used to execute commands when a shell exits.
Good time to clean up caches or tmp files/folders.
Logout of sessions, set the environment back to how you like it when you login.

Send an email to yourself of temp logs and an email to your mom telling her you love her.

References, sources & points of interest.

  1. http://zsh.sourceforge.net/Doc/Release/Files.html#Files
  2. https://unix.stackexchange.com/questions/71253/what-should-shouldnt-go-in-zshenv-zshrc-zlogin-zprofile-zlogout#71258
  3. https://thevaluable.dev/zsh-install-configure/

Oldest comments (2)

Collapse
 
roguescholar profile image
Peter J. Mello

You kind of contradict yourself in the zshenv section when you say that the file is required to be in $HOME. Shortly thereafter you suggest setting a new value for $ZDOTDIR which is the actual location that .zshenv is sourced from and for which $HOME is simply the default value. It's also advisable to use a conditional brace expansion to set $ZDOTDIR, such as ${XDG_CONFIG_DIR:-$HOME/.config}/zsh.

Collapse
 
alephalpha0 profile image
alephalpha0

Thank you for the critique, but from my experience using Termux, Manjaro, Mint, Arch, and Puppy variants of Linux: ZSH will always source .zshenv from $HOME, regardless if XDG_CONFIG*, ZDOTDIR, or any other env variable is set to something user specific. I might just have been lucky, I don't know. I also don't wanna attempt to read the entire ZSH manual to find out. :)