How do 3rd-party programs modify the $PATH variable

bashcatalinacommand lineterminalzsh

When I echo my $PATH variable, I see the binaries of some programs that I have previously uninstalled, and conversely, I do not see the path to some of the binaries that I can use, such as code.

I am using the default zsh that comes pre-installed with macOS Catalina, and from my understanding, $PATH must be set in either one of its "main" start-up files in /etc/ or a user configured override commonly found in the users home directory.

Yet, there has to be more because when I run:

cat /etc/zsh* ~/.zshrc /etc/profile /etc/bash* | grep PATH

## only ~/.zshrc is included because it's the only one in my home directory

The only result that is returned is the PATH variable I exported in my ~/.zshrc file. This definition does not contain the uninstalled binaries that are still there when I echo $PATH or the "hidden" binaries that are not there when I echo $PATH but can still be executed anywhere from the command line.

So my questions are:

  1. There must be some other file that I am not seeing that is updating the $PATH variable before my own user-configured file does. Where can this file be, if not the start-up files caught by my previous cat command?

  2. I guess you don't have to update the $PATH variable with a binary's location to be able to run it anywhere in the terminal. If that is the case, how is this done?

Best Answer

You can check the content of /etc/paths and the files in /etc/paths.d which gets added to PATH by

if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi

in /etc/profile (or /etc/zprofile). Run /usr/libexec/path_helper -s to see what it does.