104

Now that I've upgraded to Catalina and I am using the new ZSH shell, I've noticed that ~/.bash_profile has been replaced with ~/.zprofile and since installing iTerm2 shell integration, it added a ~/.zshrc file.

Looking at the ZSH documentation on Startup/Shutdown Files, there are a number of files (located in the home directory $HOME or ~/):

  • .zprofile (login shell)
  • .zshenv (environment variables)
  • .zshrc (interactive shell)
  • .zlogin (login shell)
  • .zlogout (when the shell exits)

What is also confusing is that ~/.zprofile and ~/.zlogin are both for login shells, so, things can get confusing as to what to put where.

What startup/shutdown files should be used when setting up the ZSH shell environment and how/what should they be configured?

1
  • The ZSH documentation you mentioned in your question, explains in detail in which sequence zsh is looking for those files. All of them are optional, and you are the master of your account: There is no law telling you which command you have to put into which file. Just put them whereever they do what you want, given the type of shell you are using (login/non-login; interactive/non-interactive). Don't forget that environment variables are inherited by their child processes. Dec 30, 2021 at 9:47

1 Answer 1

126

This is an attempt to write a canonical QA for this issue, as per the Meta post: Where is the list of canonical questions stored for Ask Different? I expect it to be periodically edited with the goal of becoming a comprehensive information resource.

What should be used in ZSH on a Mac

I posted a more narrowly scoped question on Unix & Linux and got some clarification on how these files "work." Here's the summary of that answer and what I've learned in my research as to what, in my opinion should be used in a ZSH environment on a Mac.

  • .zprofile

    .zlogin and .zprofile are basically the same thing - they set the environment for login shells; they just get loaded at different times (see below). .zprofile is based on the Bash's .bash_profile while .zlogin is a derivative of CSH's .login. Since Bash was the default shell for everything up to Mojave, stick with .zprofile.

  • .zshrc

    This sets the environment for interactive shells. This gets loaded after .zprofile. It's typically a place where you "set it and forget it" type of parameters like $PATH, $PROMPT, aliases, and functions you would like to have in both login and interactive shells.

  • .zshenv (Optional)

    This is read first and read every time. This is where you set environment variables. I say this is optional because is geared more toward advanced users where having your $PATH, $PAGER, or $EDITOR variables may be important for things like scripts that get called by launchd. Those run under a non-interactive shell so anything in .zprofile or .zshrc won't get loaded. Personally, I don't use this one because I set the PATH variable in my script itself to ensure portability.

  • .zlogout (Optional)

    But very useful! This is read when you log out of a session and is very good for cleaning things up when you leave (like resetting the Terminal Window Title)

For an excellent, in-depth explanation of what these files do, see What should/shouldn't go in .zshenv, .zshrc, .zlogin, .zprofile, on Unix/Linux.


Some Caveats

Apple does things a little differently so it's best to be aware of this. Specifically, Terminal initially opens both a login and interactive shell even though you don't authenticate (enter login credentials). However, any subsequent shells that are opened are only interactive.

You can test this out by putting an alias or setting a variable in .zprofile, then opening Terminal and seeing if that variable/alias exists. Then open another shell (type zsh); that variable won't be accessible anymore.

SSH sessions are login and interactive so they'll behave just like your initial Terminal session and read both .zprofile and .zshrc

Order of Operations

This is the order in which these files get read. Keep in mind that it reads first from the system-wide file (i.e. /etc/zshenv) then from the file in your home directory (~/.zshenv) as it goes through the following order.

.zshenv.zprofile.zshrc.zlogin.zlogout

10
  • 2
    Zsh is a UNIX command interpreter (shell) usable as an interactive login shell and as a shell script command processor. Of the standard shells, zsh most closely resembles ksh but includes many enhancements. It does not provide compatibility with POSIX or other shells in its default operating mode. Zsh has command line editing, builtin spelling correction,programmable command completion, shell functions (with autoloading), a history mechanism, and a host of other features.
    – fd0
    Apr 17, 2020 at 17:49
  • What version of KSH uses a file named .login? ATT KSH cares about /etc/profile, ~/.profile, and whatever file is referenced by $ENV, or ~/.kshrc if $ENV is not set. MKSH cares about profile and substitutes mkshrc. Were I you, I'd lose the references to KSH and Bash, neither is relevant to the subject of your FAQ. Aug 12, 2020 at 20:48
  • 1
    Not all versions of osx had bash as the default shell before 10.3 or4 had a csh based one
    – mmmmmm
    Nov 17, 2020 at 13:01
  • 4
    There is a big caveat to modifying PATH in .zshenv which I detail here: apple.stackexchange.com/q/432226/105377
    – rgov
    Dec 9, 2021 at 16:55
  • 1
    See also to understand the difference between a login shell and an interactive shell: stackoverflow.com/questions/18186929/…
    – stwr667
    Jun 27 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .