Export path in .profile on mac

tipsywacky picture tipsywacky · Dec 20, 2012 · Viewed 72.8k times · Source

I can't believe there isn't any tutorials about this after googling. Can someone point me a direction or explain what these lines and variables mean in .profile on mac? How can someone configure them?

export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
#export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
export PATH=$PATH:/usr/local/mysql/bin
#export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
#export PATH=/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php5/bin:/Applications/MAMP/htdocs/cake/cake/console:/opt/local/bin:/opt/local/sbin:$PATH
export PATH=/android-sdks/platform-tools:~/android-sdks/tools:$PATH
export PATH=/Users/android-sdks/platform-tools:/Users/android-sdks/tools:$PATH

Answer

GargantuChet picture GargantuChet · Dec 20, 2012

Any line starting with # is a comment. PATH= sets the value of the PATH variable, $PATH expands to the current value, and the export at the beginning makes the value available to programs that you run from the terminal.

Let's assume that the initial value of PATH is /usr/bin:/bin.

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

Replace $PATH with the previous value of $PATH. So the above sets PATH to

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

Moving forward,

export PATH=$PATH:/usr/local/mysql/bin

Again, replace $PATH with the previous value:

/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/local/mysql/bin

After

export PATH=/android-sdks/platform-tools:~/android-sdks/tools:$PATH

we have

/android-sdks/platform-tools:~/android-sdks/tools:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/local/mysql/bin

(The ~ is a special token that will be replaced with the path to your home directory, but you should get the picture.)

And finally, after

export PATH=/Users/android-sdks/platform-tools:/Users/android-sdks/tools:$PATH

we get

/Users/android-sdks/platform-tools:/Users/android-sdks/tools:/android-sdks/platform-tools:~/android-sdks/tools:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/local/mysql/bin

PATH is used to find commands that you enter on the command line. So if you type foo, the shell will look for the names below, in the order specified:

/Users/android-sdks/platform-tools/foo
/Users/android-sdks/tools/foo
/android-sdks/platform-tools/foo
~/android-sdks/tools/foo
/opt/local/bin/foo
/opt/local/sbin/foo
/usr/bin/foo
/bin/foo
/usr/local/mysql/bin/foo

To see the current value of PATH, run

echo $PATH

Should you add new directories to the beginning of PATH, or to the end? It doesn't always matter, but sometimes it does. It depends on which locations you want the system to check first.

You should add to the beginning if the goal is to search other directories before looking in the default ones. For example, git is a utility used to manage source code. If I wanted to install a newer version of git in ~/git and use that by default, I'd do:

export PATH=~/git/bin:$PATH

This means that ~/git/bin/git would override /opt/local/bin/git.

On the other hand I might prefer to have the administrator install git system-wide. If I want to use my local copy only until the administrator makes it available for everyone, then this makes more sense:

export PATH=$PATH:~/git/bin

In this case ~/git/bin/git would only be used if /opt/local/bin/git didn't already exist since /opt/local/bin is earlier in the path.

A lot of commands won't exist in more than one place. For example you might write my-backup-script and put it on an Apple Time Capsule at /Volumes/Capsule/scripts. It's not likely that there would be a command called my-backup-script in any other location to conflict. So either of the following commands would let you type my-backup-script and run the right script:

export PATH=/Volumes/Capsule/scripts:$PATH

or

export PATH=$PATH:/Volumes/Capsule/scripts

Is one better? My advice would be to do the latter. Remember that system will need to search the directories in $PATH, in order, for every command that it needs to find. Most commands will be on the local system, so it makes the most sense to put slower network storage at the end of the path.