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?
Best Answer
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 ofabc
in one of the paths defined in PATH will be executed.To access or print the variable PATH you have to use $PATH.
Example:
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:
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
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:
The resulting PATH is
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
or
The first entry preferably executes a command
abc
found in the /bin folder of the user, the second one a commandabc
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:
3. Temporary paths
By issuing
export PATH="/Users/username/bin:$PATH"
orexport 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 Terminalexport PATH="/Users/username/bin2:$PATH"
then the resulting PATH is:The file ~/.bash_profile contains
export PATH="$PATH:/Users/username/bin"
and you issue in Terminalexport PATH="/Users/username/bin2:$PATH"
then the resulting PATH is: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.