Mac OS X Change $PATH Variable Guide explains how to change the system PATH variable in Apple OS X Mavericks and Mountain Lion from terminal bash. Normally, we do not need to change the system PATH. Questions about the settings of environment variables
and the PATH are quite common. A simple explanation of what PATH mean to the OS, as well as simple guidelines on how to set and read them accordingly are rightly shown in this Mac OS X Change $PATH Variable Guide.
Mac OS X Change $PATH Variable Guide : Before We Start
We usually use iTerm2 instead of Terminal. It really does not matter which Terminal Program you are using (You can read about Terminal here). For example, while using make Command on OSX to Compile Application, we need the PATH. Environment Variables in Linux are prefixed with a dollar sign ($) such as $HOME or $HOSTNAME. Many well-known and standard variables are spelled out in capital letters to signify just that. Keep in mind that variable names are case sensitive meaning that $User and $USER are entirely unrelated from the shell’s point of view.
Unix derivatives define system wide variables in shell-scripts located mostly in the /etc folder, but user-specific values may be given to those variables in scripts located in the home folder (e.g /etc/profile, $HOME/.bash_profile). The .profile file in the home folder is a common place to define user variables.
---
These files are regular shell scripts and can contain more than just environment variable declarations. To set an environment variable, use export. To show your currently defined environment variables in a terminal, run env. The export command is a standard way to define variables. The syntax is very intuitive. The outcome is identical for these two lines :
1 2 | var=value; export var export var=value |
Mac OS X Change $PATH Variable Guide : Steps
In OS X, hidden files are those which starts with a period – .profile or .bashrc can be examples. You can not open them in GUI (i.e. from Finder). Vim is the text editor which is known as vi in command line tools. Irrespective of what Terminal application (iTerm2 or Terminal) you are using, you can open then in vi and edit.
If you run this command :
1 | echo $PATH |
You will get the current path. You got the current path, now you can use the previous command to set the path to easier one. In the previous guide on Customize iTerm2 with Color Schemes, Syntax Highlighting, basically we did a kind of ugly cheat, but in these situations, you’ll either need to know the location of :
.bash_profile
or
Edit from the /etc/paths.d directory. Apple has path_helper utility. In the first case, which is more commonly used – we find the .bash_profile file and open it in vi. You must note that, the first way of editing PATH variable is for a single user account but the second way is for the whole OS – thereby all the users. Guide for the second way is here :
1 | https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man8/path_helper.8.html |
For the first way, a typical example usage is, by opening the .bash_profile file and editing it, in this example, we are adding /new/dir/location directory to $PATH variable. :
1 2 3 | vi $HOME/.bash_profile export PATH=$PATH:/new/dir/location source $HOME/.bash_profile |