macos – When opening terminal applications, `launchd` is setting the initial `$PATH` inconsistently. Why?


I recently discovered that launchd on macOS provides an initial, default $PATH to new terminal windows that is not the default path set by macOS at log in, but instead is produced by launchd according to (presumably) its own internal logic — without changes ever having been made via either launchctl or any .plist file that I could find.

This is the case whether using the Terminal.app distributed by Apple, or a third-party terminal such as Alacritty.app.

The default path on macOS at log in, as can be verified with sysctl -n user.cs_path, is

/usr/bin:/bin:/usr/sbin:/sbin

But when I placed echo $PATH on the first line of ~/.zshenv, which is sourced before /etc/zprofile and any other shell configuration file, I was seeing an entirely different $PATH when opening a new window in Alacritty:

/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin

Which I verified to be coming from launchd by searching the output of launchctl dumpstate:

pid/72911 = {
    type = pid
    originator = /Applications/Alacritty.app
    creator = alacritty[72911]
    ...
    environment = {
        PATH => /usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin
        ...
    }
        ...
}

After restarting Alacritty, with only one window and one instance of the shell the same echo command produced:

/usr/bin:/bin:/usr/sbin:/sbin

Which is what it should be. But then, opening the default Terminal.app, I get

/usr/bin:/bin

Which is, again, not the OS default.

This doesn’t necessarily affect my ability to use the shell because I can set $PATH in .zshenv or .zprofile to whatever I’d like, but it does require that I override the $PATH that launchd sets if I wish to control search order. In fact, what launchd is doing makes controlling search order crucial because there is apparently no guarantee that the initial $PATH handed to the shell will be consistent.

Does anybody know why launchd is providing an inconsistent $PATH? Is there any rhyme or reason to what it decides to include or not include in the $PATH that it provides to every shell created with every new terminal window, and varying it by terminal application?

Again, I have never

  1. issued launchctl setenv PATH
  2. issued either sudo launchctl config user or config system
  3. modified any .plist file anywhere (or found any that set the PathEnvironmentVariable key).

I am running macOS Sequoia 15.4 (24E248) on an M2 MacBook Pro, using the stock Zsh shell.

The closest answer I could find mentions that launchd manipulates the default $PATH, but the author doesn’t specify how or why.

Leave a Reply

Your email address will not be published. Required fields are marked *