MacOS – Set PATH variable so that it is detected in all applications, even outside terminal. (El Captain)

emacsenvironment-variablesmacospathtemperature

My ~/etc/path looks as follows:

/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin

The Problem:

When I type echo $PATH into terminal, I get back:

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

And this persists for every application started in the terminal. For instance, if I type emacs, and then do:

M-x getenv PATH

My output is:

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

However, if I start up emacs using launchpad instead of terminal and once again enter M-x getenv PATH, my output is instead /usr/bin:/bin:/usr/sbin:/sbin.

As you can see my change to ~/etc/path, which was the addition of /usr/local/bin, is missing.

Another Example:

I use a Firefox plugin called vimperator which emulates vim functionality in Firefox. It has a feature that allows the user to edit webpage input in vim. However, this does not seem to work on OS X. On a hunch I type the command !echo $PATH into vimperator and see the following: /usr/bin:/bin:/usr/sbin:/sbin. Of course, since the symlink called gvim, which points to macvim, was created and placed into /usr/local/bin by brew, this behavior makes sense. What I don't understand is why the PATH is read the way it is.

Solutions that I have tried:

  • I have obviously restarted my machine.

EDIT (As requested by now deleted comment):

using sysctl user.cs_path in the terminal brings up:

user.cs_path: /usr/bin:/bin:/usr/sbin:/sbin

Best Answer

The loginwindow process sets a user's environment. It sets the variables TMPDIR and PATH, consults Directory Services and sets SHELL HOME USER LOGNAME . PATH is set to /usr/bin:/bin:/usr/sbin:/sbin unless modified by launchd (in the more current versions of OS X). This is the PATH variable available to the user's processes unless modified by that particular process (cron as an example sets the PATH to /bin:/usr/bin).

Terminal emulator applications start your shell as a login shell thus the PATH is modified by reading the various shell startup file. Any process started by your shell now inherits the new PATH variable. Other applications using a shell do not read any shell config files and use those environment variables available in the current environment. An application may modify the environment by using the LSEnvironment key in their Info.plist.

As of Yosemite, you can change the PATH variable in the following form.

sudo launchctl config user path <path>

in your case you want /usr/local/bin at the front of the path.

sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

reboot your system and the new PATH will be set by loginwindow.