Setting Environment Variables with Spaces in OS X 10.8 (Mountain Lion)…

For setting the PATH and other environment variables, see here, since it will explain the more appropriate place for them.

Unfortunately, the methods listed in the linked article above don’t work for environment variables with spaces, e.g. MAVEN_OPTS="-Djavax.net.ssl.trustStore=keystore -Djavax.net.ssl.trustStorePassword=password".

To set these, the variables have to be configured as launch daemon arguments.  Here is an example of a file I have:

$ cat /Library/LaunchDaemons/mavenopts.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
	"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>MAVEN_OPTS</string>
	<key>ProgramArguments</key>
	<array>
		<string>launchctl</string>
		<string>setenv</string>
		<string>MAVEN_OPTS</string>
		<string>-Djavax.net.ssl.trustStore=keystore -Djavax.net.ssl.trustStorePassword=password</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>KeepAlive</key>
	<true/>
	<key>LaunchOnlyOnce</key>
	<true/>
</dict>
</plist>

This file can also go in /System/Library/LaunchDaemons instead of /Library/LaunchDaemons, but this is more a matter of philosophy; either location will work.

For more details, here is the Apple documentation: http://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html#//apple_ref/doc/uid/10000172i-SW7-BCIEDDBJ

Setting Environment Variables in OS X 10.8 (Mountain Lion)

I was recently trying to install SQL Developer from Oracle and wanted the LDAP function to work, which would require the application to be aware of my ORACLE_HOME, but I could not get the application to become aware of the environment variable when I launched it from the Launcher or the Dock.

Shell Variables

For environment variables that are only going to be used in the terminal, setting them in the .bash_profile is sufficient.  I’m not totally sure how the OS X shell works, but it seems to only read the .bash_profile (doesn’t seem to read .bashrc etc.), and it reads it every time a new terminal is open.

Example

PATH=${PATH}:${HOME}/scripts; export PATH

The problem with using the .bash_profile is that applications in the GUI are not aware of the environment variables (e.g. in my case, launching SQL Developer from the command line activated the features I wanted, but clicking the icon from the Launcher did not activate the features).

Environment Variables

There used to be a mechanism for setting environment variables prior to Mountain Lion by setting them in the ${HOME}/.MacOSX/environment.plist file, but this doesn’t seem to work in Mountain Lion.  They have to be set via launchd in /etc/launchd.conf. Unfortunately, this can’t be done at a user level at the moment (${HOME}/.launchd.conf not currently supported), so it must be done at a global level (affects all users).

Example

setenv ORACLE_HOME /opt/oracle

Path Variables

There is one more way to set the PATH globally for all users. It can be done by adding directories that should be in the path into files under the /etc/paths.d directory.

Example

Here is an example of a file that might be called /etc/paths.d/oracle.

/opt/oracle/bin