Order of execution and purpose of .profile vs .zshrc
Few questions:
- What is a distinct purpose of
.profile
and.zshrc
? - What gets called first when the shell comes to life?
~/.profile
is only loaded when running zsh
in sh
compatibility mode, otherwise it reads .zprofile
, as well as .zshrc
for interactive and .zlogin
for login shells.
Their purpose develops from when they're loaded. As .zshrc
is loaded for interactive shells, set the aliases etc. you expect in such cases there.
.profile
has the additional property that it's loaded by several other shells as well (e.g. bash
, unless it finds something more specific; or ksh
) in one way or another, so you can store configuration for multiple shells in this file.
Relevant excerpts from man zshall
, emphasis mine:
STARTUP/SHUTDOWN FILES
Commands are first read from/etc/zshenv
; [...]Commands are then read from
$ZDOTDIR/.zshenv
. If the shell is a login shell, commands are read from/etc/zprofile
and then$ZDOTDIR/.zprofile
. Then, if the shell is interactive, commands are read from/etc/zshrc
and then$ZDOTDIR/.zshrc
. Finally, if the shell is a login shell,/etc/zlogin
and$ZDOTDIR/.zlogin
are read.COMPATIBILITY
Zsh tries to emulate sh or ksh when it is invoked as sh or ksh respectively; [...]The usual zsh startup/shutdown scripts are not executed. Login shells source
/etc/profile
followed by$HOME/.profile
. If the ENV environment variable is set on invocation,$ENV
is sourced after the profile scripts. The value ofENV
is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a pathname. Note that the PRIVILEGED option also affects the execution of startup files.
The zsh manual, section 5 "Files" [1] has a section that shows the precise sequence of dot files read at login and logout. This is specific to zsh, and does not cover the interaction with .profile
, ie, compatibility. Nonetheless, I have found this helpful in structuring my zsh dot files.
[1] http://zsh.sourceforge.net/Doc/Release/Files.html#Startup_002fShutdown-Files