In Mac OSX there are a few ways to set environment variables:
1. in TERMINAL
2. in .bash_profile
3. in environment.plist
1. The TERMINAL way to set environment variables
Environment variables can be set in Mac OS X by simply opening a terminal window (Applications -> Utilities -> Terminal) and entering the following (for example)
$ export M2_HOME=/usr/local/apache-maven/apache-maven-3.0.3
If you need to make a variable available to an application, then you can launch the application from the terminal window.
However, those variables will only be available to that session of the terminal. When you close the terminal window, the variable will be gone. To make environment variables available to future sessions, then you can either set them in .bash_profile or environment.plist
2. The .bash_profile way to set environment variables
This is probably the easiest way to set environment variables so that they are available to all future TERMINAL sessions.
Open a new TERMINAL window and create a new .bash_profile file if you don’t have one already:
$ cd ~
$ touch .bash_profile
Now open the .bash_profile file with your favorite editor. You can find the file in this location:
Macintosh HD/Users/<your username>/.bash_profile
Now you can enter your environment variables in the new file:
If you don’t see this file then you need to make your hidden files visible! Open a new TERMINAL window and enter the following:
$ defaults write com.apple.finder AppleShowAllFiles TRUE
$ killall Finder
This will show you all of the hidden files and folders on your operating system. If you want to reverse the command replace TRUE with FALSE.
3. The environment.plist way to set environment variables
If you are looking for a visual tool to add, delete, and edit environment variables then you can install the XCode tools. It seems like a lot more trouble than it is worth in my opinion, but you can decide that yourself!
The Apple development environment toolset is called Xcode.To install XCode tools, do the following….
Put your MacOSX install disk into your drive. XCode should be included with all releases of Mac OS X but I can’t say that for sure.
When you open up the install DVD, you’ll see that in addition to the cool “Install Mac OS X” icon, there’s also a folder labeled “Optional Installs”. That’s what you want.
There’s an icon there labeled “Optional Installs.mpkg”. That’s not what you want, so don’t click on it. Instead, go into the folder “Xcode Tools”, where you’ll see three installers: “Dashcode.pkg”, “WebObjects.mpkg” and “XcodeTools.mpkg”. Double-click on “XcodeTools.mpkg” to get started.
Now that you installed XCode, you can create your environment.plist file! Here’s how:
$ cd? ~
$ mkdir .MacOSX?
$ echo “” > .MacOSX/environment.plist
$ open .MacOSX/environment.plist
This should open your new, empty environment.plist file in the Property List Editor. Select Root and Add a Child: the Key should be the name of the variable you want to set; Type should be String; Value should be the value you want to set for this variable. Next time you open a new TERMINAL session, the variables should be set.
Environment variables set in environment.plist also appear in the standard bash environment. You can see all your environment variables using the shell command env from a TERMINAL window. (but you must restart the computer… that is a pain!)
Creating the TEST variable and restarting the computer and typing “env” into the terminal should result in the TEST variable being there.