简体   繁体   中英

How to set environment variables in Yosemite OSX?

I tried setting up environment variables as explained in: https://developer.apple.com/library/mac/documentation/MacOSX/Conceptual/BPRuntimeConfig/Articles/EnvironmentVars.html

But it didn't help. Writing .bash_profile is a workaround for executing an application via command line but IDEs like IntelliJ, Eclipse are not working with this setup.

Is there an another work around for setting up environment variable in Yosemite?

Create file named .profile in home directory - by opening a Terminal and using command “touch .profile” and Close Terminal. Open Terminal again. Open that file in a plain-text editor (nano / vim in a Terminal window (current directory as home directory)). Insert lines as “export VARIABLE_NAME=VARIABLE_VALUE” . Save, exit editor and close Terminal. Open Terminal and issue the command env to see all environment variables. Check the ones you exported. But environment variables defined in .profile are not passed to GUI applications. A workaround For GUI Applications . Use command “open -a Appname” in a Terminal session to start the GUI App.

Another simple approach which works for all applications invoked from particular user : This approach works in 10.10.4 (atleast which i can verify not sure in other versions) create /Users/your_user/Library/LaunchAgents/custom.startup.plist with content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key>
  <string>my.startup</string>
  <key>ProgramArguments</key>
  <array>
    <string>sh</string>
    <string>-c</string>
    <string>launchctl setenv VARIABLE_NAME1 VARIABLE_VALUE1 | launchctl setenv VARIABLE_NAME2 VARIABLE_VALUE2 | launchctl setenv VARIABLE_NAME3 VARIABLE_VALUE3</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
</dict>
</plist>

logout and login again

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM