# Set environment variables here so they are available globally to all apps# (and Terminal), including those launched via Spotlight.## After editing this file run the following command from the terminal to update# environment variables globally without needing to reboot.# NOTE: You will still need to restart the relevant application (including# Terminal) to pick up the changes!# grep -E "^setenv" /etc/launchd.conf | xargs -t -L 1 launchctl## See http://www.digitaledgesw.com/node/31# and http://stackoverflow.com/questions/135688/setting-environment-variables-in-os-x/## Note that you must hardcode the paths below, don't use environment variables.# You also need to surround multiple values in quotes, see MAVEN_OPTS example below.#setenv JAVA_VERSION 1.6setenv JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6/Homesetenv GROOVY_HOME /Applications/Dev/groovysetenv GRAILS_HOME /Applications/Dev/grailssetenv NEXUS_HOME /Applications/Dev/nexus/nexus-webappsetenv JRUBY_HOME /Applications/Dev/jruby
setenv ANT_HOME /Applications/Dev/apache-antsetenv ANT_OPTS -Xmx512M
setenv MAVEN_OPTS "-Xmx1024M -XX:MaxPermSize=512m"setenv M2_HOME /Applications/Dev/apache-maven
setenv JMETER_HOME /Applications/Dev/jakarta-jmeter
Save your changes in vi and reboot your Mac. Or use the grep/xargs command which is shown in the code comment above.
Prove that your variables are working by opening a Terminal window and typing export and you should see your new variables. These will also be available in IntelliJ IDEA and other GUI applications you launch via Spotlight.
;;; Provide support for the environment on Mac OS X
(defun generate-environment ()"Dump the current environment into the ~/.MacOSX/environment.plist file.";; The system environment is found in the global variable:;; 'initial-environment' as a list of "KEY=VALUE" pairs.(let ((list initial-environment)pair start command key value);; clear out the current environment settings(find-file "~/.MacOSX/environment.plist")(goto-char (point-min))(setq start (search-forward "<dict>\n"))(search-forward "</dict>")(beginning-of-line)(delete-region start (point))(while list(setq pair (split-string (car list) "=")list (cdr list))(setq key (nth 0 pair)value (nth 1 pair))(insert " <key>" key "</key>\n")(insert " <string>" value "</string>\n")
;; Enable this variable in launchd(setq command (format "launchctl setenv %s \"%s\"" key value))(shell-command command));; Save the buffer.(save-buffer)))
EnvPane是Mac OS X 10.8(Mountain Lion)的首选项窗格允许您在两个图形中为所有程序设置环境变量和终端会话。它不仅恢复了对~/。MacOSX/environment.plistMountain Lion中,它还发布您的立即更改环境,无需注销然后回来。EnvPane包括(并自动安装)一个启动代理1)登录后早期运行,2)每当~/。MacOSX/environment.plist更改。代理读取~/. MacOSX/environment.plist并导出环境变量通过相同的API将该文件传输到当前用户的启动实例使用的是Launchctl setenv和Launchctl unsetenv。
The above will overload the Bash builtin "export" and will export everything normally (you'll notice I export "export" with it!), then properly set them for OS X app environments via launchctl, whether you use any of the following:
This way I don't have to send every variable to launchctl every time, and I can just have my .bash_profile / .bashrc set up the way I want. Open a terminal window, check out your environment variables you're interested in with launchctl getenv myVar, change something in your .bash_profile/.bashrc, close the terminal window and re-open it, check the variable again with launchctl, and voilá, it's changed.
Again, like the other solutions for the post-Mountain Lion world, for any new environment variables to be available for apps, you need to launch or re-launch them after the change.
/etc/launchd.conf未在OS X v10.10(Yosemite)、OS X v10.11(El Capitan)、macOS v10.12(Sierra)或macOS v10.13(High Sierra)中使用。
从launchctl手册页:
/etc/launchd.conf file is no longer consulted for subcommands to run during early boot time;this functionality was removed for security considerations.