Where does $PATH get set in OS X 10.6 Snow Leopard?

141

92

I type echo $PATH on the command line and get

/opt/local/bin:/opt/local/sbin:/Users/andrew/bin:/usr/local/bin:/usr/local/mysql/bin:/usr/local/pear/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/opt/local/bin:/usr/local/git/bin

I'm wondering where this is getting set since my .bash_login file is empty.

I'm particularly concerned that, after installing MacPorts, it installed a bunch of junk in /opt. I don't think that directory even exists in a normal Mac OS X install.

Update: Thanks to jtimberman for correcting my echo $PATH statement

Andrew

Posted 2009-11-11T18:48:39.547

Reputation: 11 982

I have made a script mostly based on this post, that quickly prints out where the PATH is defined along these multiple locations: https://gist.github.com/lopespm/6407349952bc8a1ff8fb

– Pedro Lopes – 2016-02-16T01:50:10.763

5

comment from SMcCandlish:easiest solution when you want to make a site-wide path change (e.g. to include /usr/libexec or whatever) is to edit /etc/paths . While the new /etc/paths.d/ thing is functional, it's actually more hassle than maintaining a single file. As others have hinted but not spelled out, if the path changes you want would be useful only for one user, it's best to make that change in the bash and/or tcsh config files in that user's home directory instead of system-wide.

– Sathyajith Bhat – 2011-10-03T10:11:20.757

@Sathya, It seems to me /etc/paths.d is useful for installation scripts. If I want my installation script to add the installed program to the path, I can drop a file in /etc/paths.d. If I want to edit the path manually, /etc/paths is a better option. – haydenmuhl – 2012-12-06T01:04:52.853

Answers

161

When bash starts it reads the following files every time you login. For the purposes of OS X, this means every time you open a new Terminal window.

/etc/profile
~/.bash_profile
~/.bash_login   (if .bash_profile does not exist)
~/.profile      (if .bash_login does not exist)

When you start a new shell by typing bash on the command line, it reads .bashrc

OS X also uses ~/.MacOSX/environment.plist to set more environment variables, including paths if necessary.

Finally, /etc/paths and /etc/paths.d are read by the shell too.


/opt/local/bin etc. are added in ~/.tcshrc by MacPorts. Also be sure to look in ~/.cshrc.

Steve Folly

Posted 2009-11-11T18:48:39.547

Reputation: 5 293

I think this answer is incomplete. I am trying to figure out where an erroneous (non-existent) path: /usr/bin/local, my shell is complaining about, and it is not in any of those files. – Tommy – 2016-08-19T02:07:14.550

I think .bashrc is read regardless if you type bash or not. – Xiao Peng - ZenUML.com – 2018-06-07T12:23:35.013

5

Apparently, ~/.MacOSX/environment.plist does not work for some people. In Change Path for OS X an alternative by using the folder /etc/paths.d is described. I've not tested that.

– Arjan – 2010-12-18T19:35:24.390

+1 for ~/.cshrc. Went nuts trying to figure out why ~/.profile wasn't working. – tomlogic – 2011-10-19T06:31:18.577

49

Take a look at the file /etc/paths, which is used by /usr/libexec/path_helper, which is used by /etc/profile.

For MacPorts, use sudo /etc/paths/opt/local/bin and reopen the terminal window.

Jim Logan

Posted 2009-11-11T18:48:39.547

Reputation:

25

Seriously, Leopard gave us a new way to add path. Just create a file containing the path part you want to add, and put it in /etc/paths.d

A quick example of doing this in one step is:

echo "/some/path/to/add" >> /etc/paths.d/somefilename

Otherwise, you can just go to /etc/paths.d and put the file there directly. Either way, any path parts in the files in that directory will be appended to the default path.

This also works for manpath.

Here's a link to more details:

ars technica: how do i add something to PATH in snow leopard?

On a 2nd note: MacPorts puts everything into the opt directory precisely because it isn't used by Apple's stuff. That way it won't conflict. Their guide (excellently written, BTW) has an explanation of why it uses opt and how to change that default if you'd like.

MacPorts Guide

leanne

Posted 2009-11-11T18:48:39.547

Reputation: 391

Single line, no appending to files and easy removal. Love this. Thanks leanne – Ibrahim Lawal – 2016-07-30T07:09:45.913

Very cool. Now we only have to get those path dependent applications to no longer pollute our profile files... – Daniel Beck – 2011-03-25T18:19:25.327

Love it. Simple! – Andrew Hedges – 2011-04-04T17:07:06.340

comment from SMcCandlish: I have to concur that there's nothing weird or bad about MacPorts using /opt (likewise Fink uses /sw); trying to replace stock Mac BSD parts with bleeding-edge builds from external projects like these is a recipe for an unusable system that requires an OS reinstallation.

– Sathyajith Bhat – 2011-10-03T10:12:10.630

4

To show your path, echo $PATH.

echo $PATH

To set your path, edit ~/.bash_profile, not ~/.bash_login.

jtimberman

Posted 2009-11-11T18:48:39.547

Reputation: 20 109

3

There's also the path as determined by ssh.

Compare echo $PATH to ssh localhost 'echo $PATH'. Since ssh does not read /etc/profile, /usr/libexec/path_helper doesn't run and thus /etc/paths is skipped. Now try ssh localhost 'source /etc/profile; echo $PATH'. The paths should be closer. The remaining differences will likely be due to path modification in your .bash_profile (which is also skipped by ssh) and .bashrc (which is read by ssh).

If you want your ssh path to be similar to your normal terminal path, you could add source /etc/profile to your .bashrc.

Keith Bentrup

Posted 2009-11-11T18:48:39.547

Reputation: 335

Thanks for the hint! I was looking exactly for the solution with ssh. svn+ssh could not find svnserve command exactly because path to svnserve was in .profile, but wasn't in .bashrc. – KIR – 2013-07-04T09:01:59.713

2

It could be defined in either:

  • System variables - /etc/paths
  • User variables - see @Steve Folly's explanation

ericn

Posted 2009-11-11T18:48:39.547

Reputation: 211

1

Regarding docs for /usr/libexec/path_helper utility, initial components for $PATH was taken from /etc/paths and by default looks like

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

for OS-X Snow Leopard

A B

Posted 2009-11-11T18:48:39.547

Reputation: 390

1

Actually it is stored in your .profile file instead of .bash_login and it is common that MacPorts will use this instead of the .bash_login file.

Also The /opt directory is usually created by MacPorts and it stores its files in this folder.

ricbax

Posted 2009-11-11T18:48:39.547

Reputation: 4 894