0

I'm having trouble understanding exactly how $PATH works?

I am on a Mac.. If I install Python with Homebrew, it will install Python into the /opt/local/bin directory.

Now assuming Homebrew didn't make any changes to my bash profile, if I run a generic Python script, that will run using the system included Python located in Library I think.

My understanding is that if I go into my bash profile and add this line:

export PATH=/opt/local/bin:$PATH

then it will add these directories to the front of the list.

I guess the question I have is, why do I add this line here. Why can't I go directly to the source which I think is $PATH and add the line manually there.
Is $PATH a file? And if so where is it located?

2
  • PATH is an environment variable. $PATH is the string you use when you want to access its contents.
    – fkraiem
    Commented Jan 1, 2016 at 1:55
  • That's the way it's supposed to be done, just do as instructed! Commented Jan 1, 2016 at 3:07

1 Answer 1

3

The shell path (i.e. PATH) for a user in OSX is a variable defining a set of locations in the file system whereby the user can use certain applications, commands and shell scripts without the need to specify the full path to that command or program in the Terminal.

If you enter abc in Terminal the first occurrence of abc in one of the paths defined in PATH will be executed.

To access or print the variable PATH you have to use $PATH.

Example:

echo $PATH

The variable PATH has a general and a user specific part.

1. General part:

The general part is defined by the (superior) file /etc/paths and by the (inferior) content of the folder /etc/paths.d.

The default content of the file /etc/paths (in OS X) is:

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

The default folder /etc/paths.d is empty. On older systems with an X11 installation the folder contains a file named 50-X11 with the content

/usr/X11/bin

which adds an additional path (/usr/X11/bin) to PATH.

The prepended number- just defines the load order.

My /etc/paths.d folder contains the following files:

/etc/paths.d/40-mysql    #file content: /usr/local/mysql/bin
/etc/paths.d/50-X11      #file content: /usr/X11/bin
/etc/paths.d/60-testdisk #file content: /usr/local/testdisk
/etc/paths.d/70-git      #file content: /usr/local/git/bin

The resulting PATH is

echo PATH$ 
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/X11/bin:/usr/local/testdisk:/usr/local/git/bin

2. User-specific part:

The user-specific part is defined by the file ~/.bash_profile. If you have created a folder bin in your user folder and installed some apps in there you can add the following line in ~/.bash_profile

export PATH="/Users/username/bin:$PATH"

or

export PATH="$PATH:/Users/username/bin"

The first entry preferably executes a command abc found in the /bin folder of the user, the second one a command abc found in the general path before executing an identical named one in the ~/bin folder.

This part of PATH defined by .bash_profile is only effective for the respective user.

To add the path entered in .bash_profile to the current PATH immediately you have to source the file once:

source ~/.bash_profile

3. Temporary paths

By issuing export PATH="/Users/username/bin:$PATH" or export PATH="$PATH:/Users/username/bin" in Terminal you can add a path temporarily.

This part of PATH is lost after closing the Terminal window, logging out or rebooting the Mac.

Examples (using the general part from above):

The file ~/.bash_profile contains export PATH="/Users/username/bin:$PATH" and you issue in Terminal export PATH="/Users/username/bin2:$PATH" then the resulting PATH is:

/Users/username/bin2:/Users/username/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/X11/bin:/usr/local/testdisk:/usr/local/git/bin
\____temp. path_____/

The file ~/.bash_profile contains export PATH="$PATH:/Users/username/bin" and you issue in Terminal export PATH="/Users/username/bin2:$PATH" then the resulting PATH is:

/Users/username/bin2:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/mysql/bin:/usr/X11/bin:/usr/local/testdisk:/usr/local/git/bin:/Users/username/bin
\____temp. path_____/

Summary

PATH (or $PATH) is defined by several files: /etc/paths, the files in /etc/paths.d and ~/.bash_profile and in case of adding a temporary path by additional RAM content.

You must log in to answer this question.