Search code examples
macosshellterminalzshlaunchctl

what does launchctl config user path do?


I was having a strange problem with PATH environment variable in MacOS that I spent several hours to debug:

  • Some time ago, when I was trying to fix the issue IntelliJ terminal PATH variable not the same with iTerm, I followed an online article and executed this:

    sudo launchctl config user path $PATH

  • Apparently this command sets and persists the value of PATH variable at that moment of time somewhere and that variable is loaded even before my shell is loaded whenever I start a new zsh session. Only recently I recognized this issue because I removed some paths location setting in my zshrc and the PATH variable still didn't reflect

  • My question is where does that command store the PATH variable value? and how does it load that value before my shell is loaded?

(For people who wonder how I fixed the issue: I executed the command again to set path to empty value: sudo launchctl config user path '')


Solution

  • The sudo launchctl config user path <...> command updates /private/var/db/com.apple.xpc.launchd/config/user.plist:

    $ cat /private/var/db/com.apple.xpc.launchd/config/user.plist
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        <key>PathEnvironmentVariable</key>
        <string>/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    </plist>
    

    Tested on my system, which is currently macOS 14.2.1 (AppleSilicon). You can replace user with system to operate on the system-wide preferences. Both require sudo, oddly enough.

    You can query launchd's current custom PATH setting (will return an empty string if you haven't configured one) with:

    launchctl getenv PATH
    

    You can query the default PATH by executing:

    sysctl -n user.cs_path
    

    To undo any customizations, you can issue:

    sudo defaults delete /private/var/db/com.apple.xpc.launchd/config/user.plist PathEnvironmentVariable
    

    (requires a reboot to take effect)