If you've ever opened Terminal and typed commands like git, node, python3, or docker, you've probably wondered how macOS instantly knows where these programs are located. After all, you didn't provide the full path to the executable—you simply typed its name.
This mechanism is very similar to the %PATH% environment variable on Windows. Instead of searching your entire disk, the shell checks a predefined list of directories in a specific order until it finds a matching executable. Understanding how this lookup works is invaluable for developers, especially when troubleshooting "command not found" errors, dealing with multiple versions of the same tool, or installing software through Homebrew. Before diving into the details, let's see how macOS performs this search behind the scenes.
How macOS Finds Executables (The Equivalent of Windows %PATH%)
If you're coming from Windows, you may be wondering:
When I type
gitornodein the Terminal, how does macOS know where the executable is?
The answer is the PATH environment variable, just like Linux and other Unix-based operating systems. It's conceptually the same as Windows' %PATH%, although the syntax is different.
Windows vs. macOS
On Windows, you can inspect the search path using:
echo %PATH%
You might see something like:
C:\Windows\System32;
C:\Program Files\Git\bin;
C:\Program Files\nodejs;
...
When you type:
git
Windows searches each directory listed in %PATH% from left to right until it finds git.exe.
macOS follows the same idea.
Run:
echo $PATH
You might see something like:
/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin
Notice that the directories are separated by colons (:) instead of semicolons (;).
When you type:
git
the shell searches these directories in order:
/usr/local/bin/opt/homebrew/bin/usr/bin/bin/usr/sbin/sbin
The first matching executable is the one that gets executed.
Which git Am I Actually Running?
It's possible to have multiple versions of the same tool installed.
For example:
Apple's built-in Git
Homebrew Git
Git installed by Xcode
A custom Git build
To see which executable is being used, run:
which git
Example output:
/opt/homebrew/bin/git
Or, to see every matching executable found in your PATH:
which -a git
Output:
/opt/homebrew/bin/git
/usr/bin/git
Because /opt/homebrew/bin appears earlier in PATH, that version takes precedence.
Another useful command is:
type git
which tells you whether git is an executable, shell builtin, alias, or shell function.
Why Homebrew Works Without Typing the Full Path
Suppose Homebrew installs Node.js here:
/opt/homebrew/bin/node
Since /opt/homebrew/bin is already in your PATH, you can simply run:
node
instead of:
/opt/homebrew/bin/node
The shell automatically locates the executable.
This is exactly the same convenience Windows provides when C:\Program Files\nodejs is included in %PATH%.
How Is PATH Configured?
The exact startup files depend on the shell you're using.
Modern versions of macOS use Zsh by default.
Common files include:
~/.zprofile
~/.zshrc
~/.zshenv
Older systems using Bash often use:
~/.bash_profile
~/.bashrc
To add a custom directory to your search path in Zsh:
export PATH="$HOME/bin:$PATH"
This prepends ~/bin to the existing PATH, meaning executables in your personal bin directory will be found before system executables.
A Small Example
Imagine your PATH looks like this:
/usr/local/bin:/opt/homebrew/bin:/usr/bin:/bin
You have:
/usr/bin/python3
and
/opt/homebrew/bin/python3
When you run:
python3
the shell checks:
❌
/usr/local/bin/python3(not found)✅
/opt/homebrew/bin/python3(found)
It stops immediately and executes that binary. It never reaches /usr/bin/python3.
This search order is why the placement of directories within PATH matters. If you unintentionally put an older or incompatible version of a tool earlier in the list, that's the version the shell will execute.
Finder vs. Terminal
One important distinction is that Finder does not use PATH to launch applications.
When you double-click Google Chrome.app or Visual Studio Code.app, Finder launches the executable directly from the application's bundle, typically:
/Applications/AppName.app/Contents/MacOS/
The PATH variable is primarily used by shells (Zsh, Bash, Fish, etc.) and command-line programs to locate executables when you invoke them by name.
Final Thoughts
The PATH environment variable is one of those foundational concepts that quietly powers your day-to-day development experience on macOS. Every time you run a command without specifying its full location, the shell relies on PATH to locate the appropriate executable. Once you understand the search order and know where different tools are typically installed, diagnosing issues like version conflicts, missing commands, or unexpected behavior becomes much easier.
Whether you're transitioning from Windows or you're new to Unix-based systems, mastering PATH is a small investment that pays off every time you use the Terminal. It's one of the essential building blocks every macOS developer should understand.