5

from zsh document, ~/.zprofile should only be sourced at login shell, and should NOT be sourced at every interactive session (for example, a new tab in a terminal).

  • I am running MacOS Big Sur 11.2.3 (20D91).

But I can confirm that my ~/.zprofile is sourced at every interactive session (a new tab). Why is that? Is this a bug? How to stop this incorrect behavior?

bmike
  • 235,889
sgon00
  • 2,314

1 Answers1

11

Each Terminal tab is a login session on macOS by default. You can run w to see that each tab/tty running a shell has a process name starting with - attached to it (which indicates a login shell).

Additional information (related to bash, but the same is applicable to other shells)

PS: If I remember correctly we have a Q&A which goes into even more detail on this including a rationale why macOS considers each Terminal window/tab to be a login shell. I didn't find it yet though...

nohillside
  • 100,768
  • You can nicely test this if you echo something in .zprofile. If you then run zsh in a zsh shell, you will come to the conclusion that it will not source .zprofile again, but yes, new tabs and windows are login shells, not so much in Linux though. And MacOS behaves the same when you switch your shell to /bin/bash. New terminal windows will always source .bash_profile or .profile, but unless to zsh bash will never source .bashrc in login shells, so that's why .bash_profile typically sources .bashrc manually. – Martin Braun Jan 24 '24 at 10:50