Sunday, 5 July 2009

MAC OS X: change the environment PATH

To add a new directory to the path, simply add it to the existing PATH line in .profile being careful to separate it from other directories there with colons and careful not to introduce unwanted spaces (everything after the space will be ignored). For example, to add the directory /mightyq/bin to the PATH shown above, the line could become any of the following examples:

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

Note that in the third example the new directory is added to the end of the PATH. You have the ability to optimize the searches your shell will do on your behalf each time you run a command by organizing your PATH logically. Putting less frequently used or really massive directories later in the path may give you a little performance boost (although these days things are pretty fast, so you have to be a little anal to really enjoy this).

If you don’t need a directory in your path, you can reverse the process by deleting the unwanted directory still taking care to preserve the no spaces, colon separation rules.

One last note, to test the change you made, you can use the echo command, but you need to make the shell reload the .profile first. Assuming you are in your home directory (if not, running ‘cd’ without any options will take you there), run these commands:

. ./.profile
echo $PATH

The first . is a shortcut to cause the shell to ’source’ or load the contents of the subsequent file as itself, in the manner that the shell uses when you login to a system or start a Terminal window. If you simply executed these commands like a shell script (bash .profile, for example) you would start a new shell, that shell would get the variable set, and at the end of running the .profile script, that new shell would cease to exist and the newly defined variables would be relegated to the missing sock universe.While . makes the variables generated from new shell ( child shell ) return to the current shell (parent shell).

No comments:

My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter