Skip to content

Instantly share code, notes, and snippets.

@darrenpmeyer
Last active July 25, 2018 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenpmeyer/01d209fb8bc2135fd0fc38c01780db2b to your computer and use it in GitHub Desktop.
Save darrenpmeyer/01d209fb8bc2135fd0fc38c01780db2b to your computer and use it in GitHub Desktop.
PATH environment variable generation on macOS

Applies to OS X 10.6 (Snow Leopard) and newer; tested through MacOS 10.13 (High Sierra)

The general advice for adding particular directories to your PATH environment variable on MacOS is to add an export PATH to your ~/.bashrc or equivalent. This has some drawbacks. For one, it only affects that shell for that user.

Additionally, sometimes installed Applications modify the system's default PATH in ways that you don't want.

Understanding how MacOS generates the PATH environment variable is instructive.

TL;DR read man path_helper and be enlightened

How does PATH get created?

  1. The system default paths are read, one per line from the file /etc/paths and added to PATH
  2. The files in /etc/paths.d are read, and the contents of each are added to PATH; the order in which the files are read is not reliable

Note that there is no variable expansion in the specified paths. If, for example, you add $HOME/bin to a file in /etc/paths.d, you're going to be disappointed. $HOME will not expand to your home directory, and you'll be left with a PATH that contains the literal string $HOME/bin.

Any user-specified paths in e.g. ~/.bashrc are added after the above complete, since those user files are executed later in the shell start cycle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment