4

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?

10

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...

15
  • 1
    Thank you very much for your answer. I have upvoted it. This is vey unfortunate.. Can I change this behavior somehow? I have some heavy scripts loading in .zprofile, it will cause every new tab takes a long time.. – sgon00 Apr 18 at 13:59
  • 2
    @sgon00 Well, env variables need to be loaded anew each time anyway, login session or not. But maybe ask a new question for this and explain the problem in more detail there, other users may have experience with this. – nohillside Apr 18 at 14:10
  • 1
    What about 100M of env variables? :) Just kidding. Although I don't want to share my scripts in .zprofile, I found some similar heavy loading ones online. Just give you some ideas. For example, this file github.com/nvm-sh/nvm/blob/master/nvm.sh is sourcecd and available in each terminal session. It wiill take some seconds. Related closed issues github.com/nvm-sh/nvm/issues/2387 and github.com/nvm-sh/nvm/issues/1978 – sgon00 Apr 19 at 2:59
  • 2
    @nohillside: A non-login shell inherits its env vars from its parent, so they shouldn't need to get loaded every time if the parent process had ones you want. e.g. on a GNU/Linux desktop, your terminal emulator (e.g. xterm or Konsole) might itself have been started from a shell that already sourced env vars, or your .xinitrc might have sourced some env-var setting stuff before starting the window manager / desktop session (which in turn lets you start terminals by clicking on stuff). So there are ways on other OSes at least to have env vars set for the whole session. (harder to change though) – Peter Cordes Apr 19 at 7:20
  • 1
    @PeterCordes There isn't. We have several questions about this on AD, any functionality which allowed this in older versions of macOS seems to have gone nowadays. – nohillside Apr 19 at 13:01

You must log in to answer this question.

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