Export path in .profile on mac
Asked Answered
R

3

26

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
Rozek answered 20/12, 2012 at 18:55 Comment(0)
A
64

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.

Agglutination answered 20/12, 2012 at 19:10 Comment(2)
So why do some people append to $PATH when clearly you should prepend?Ingather
One should prepend if the goal is to search a specific directory before searching the rest of the places mentioned in PATH. If someone chooses to append, they may have had different goals. Updated the answer with some details.Agglutination
K
8

From the man page, export simply sets an environment variable.

The PATH environment variable tells your Mac where to look for commands that you type on the command line, in the order that it should look. It is a list of paths, delimited by a colon.

The value $PATH is the current value of the PATH environment variable.

Thus, a line like

export PATH=/foo:$PATH

prepends /foo to the PATH

and

export PATH=$PATH:/foo

appends /foo to the PATH

Kershaw answered 20/12, 2012 at 18:59 Comment(0)
M
5

Run man

And then answer the question as export

You can see the current PATH value and observe that there are more than one path present.

(mine was /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin)

Observe that : represents end of a single path.

You can retrieve this by using $PATH

Now when you want to add one more path to this list then you need to append current path to new path like /newPath:$PATH or $PATH:/newPath.

And at last for saving it you need to use export PATH=$PATH:/newPath.

Note : This answer is for novice developers.

Medullary answered 6/12, 2016 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.