5

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?

9
  • What have you set in your iterm profile to start zsh?
    – mmmmmm
    Commented Mar 4, 2015 at 18:23
  • I don't. zsh automatically sources that file when started
    – nw.
    Commented Mar 4, 2015 at 18:27
  • Why does iTerm start zsh - the default is bash - i.e. what have you changed?
    – mmmmmm
    Commented Mar 4, 2015 at 18:34
  • 1
    It runs the users default shell in /etc/passwd
    – nw.
    Commented Mar 4, 2015 at 18:43
  • I haven't changed any of the iterm preferences at all
    – nw.
    Commented Mar 4, 2015 at 18:46

1 Answer 1

8

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

By default iTerm runs the default shell as a login shell. By default for old versions of OSX the shell is bash but the user has changed this to zsh, for newer versions of macOS the default shell is 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 the following files are sourced

/etc/zshenv
~/.zshenv
/etc/zprofile  # This is in later versions of macOS
              # Not in Mojave but in Monterey
~/.zprofile
/etc/zshrc    # This is in later versions of macOS
~/.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
/etc/zshrc    # This is in later versions of macOS
~/.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 interactive shell reads ~/.bashrc and thus the normal bash use is to source ~/.bashrc in ~/.bash_profile so to stop the need to duplicate code.

You must log in to answer this question.

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