ITerm2 runs the .zshrc twice

itermzsh

When starting a terminal in iTerm 2, my .zshrc is executed twice. Possibly has something to do with the "last login" printed at the top of terminal by iTerm? Anyone know how to fix this?

Example output, new terminal:

last login: Wed Mar  4 12:55:35 on ttys000

The mark of a good party is that you wake up the next morning wanting to
change your name and start a new life in different city.
        -- Vance Bourjaily, "Esquire"

You never know what is enough until you know what is more than enough.
        -- William Blake

~ ❯❯❯ zsh

Oh, love is real enough, you will find it some day, but it has one
arch-enemy -- and that is life.
        -- Jean Anouilh, "Ardele"

~ ❯❯❯

As you can see, when opening iTerm fortune (in my .zshrc) is executed twice, but when starting a new shell with the terminal already open it is correctly ran once. Any way to fix this behavior?

Best Answer

Much of this depends on the setup you have done in ITerm and setting up shells.

By default iTerm runs the shell in /etc/passwd as a login shell. By default the shell is bash but the user has changed this to zsh

zsh has several files that are used at startup see zsh manual $ZDOTDIR is by default your home directory

Commands are first read from /etc/zshenv; this cannot be overridden. ... ...

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.

So when items starts a login shell (and assume no global files in /tc) the following files are sourced

/etc/zshenv
~/.zshenv
~/.zprofile
~/.zshrc
~/.zlogin

If zsh is started from a shell prompt it is not a login shell and so only the following are read

/etc/zshenv
~/.zshenv
~/.zshrc

If you get different behaviour in the two cases then look at your ~/.zprofile and ~/.zlogin .

In this case one of them probably is sourcing ~/.zshrc. Note that this differs from bash where a login shell reads ~/.bash_profile and a non login shell reads ~/.bash_profile and this the normal bash use is to source ~/.bashrc in ~/,bash_profile so to stop the need to duplicate code.