How do I set environment variables on Mac OS X?

368 次查看(过去 30 天)

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2023-2-27
There are multiple ways of setting environment variables on OS X, but the most reliable and supported way is to use a plist file and have system's launchctl service load it at login. This method is described below; for other methods please consult OS X product documentation.
Create a new .plist file. The name doesn't matter, but the extension must be .plist. For example, "SampleVariable.plist".
Save the file in ~/Library/LaunchAgents/ so it is read when when you log in with your specific username.
Paste the following contents into the .plist file you just created:
<?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>Change To Label Of Your Choice</string>
<key>ProgramArguments</key>
<array>
<string>/bin/launchctl</string>
<string>setenv</string>
<string>Variable Name You Want To Set</string>
<string>Variable Value You Want to Set</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
There are three things you need to change before saving the .plist file: Label, Variable Name, and Variable Value. Make the changes, save the file and reboot(or see alternate method below) your Mac for the changes to take effect.
Please note this sets a user specific variable which will apply only to the user saving this file in their home directory.
Sample .plist file can be downloaded below.
------------------------------------------------------------------------------------------------------------------------------------------------
How the plist works:
This file tells /bin/launchctl to run commands in the next 3 strings below. Setenv is used to set an environment variable in OS X Terminal. Variable name has to be specific to whatever you are trying to set, it cannot be a random name you pick. For example, it can be either MLM_LICENSE_FILE or MATLAB_JAVA. Variable value has to indicate the path or parameter you want to change, such as port@host for MLM_LICENSE_FILE or path to a different version of Java for MATLAB_JAVA.
Alternate method to load the plist into your user account:
Instead of rebooting your computer you can run the following command in Terminal: launchctl load ~/Library/LaunchAgents/EnterNameOfYourFileHere.plist
This will load it into the system, but to check for it you will need to close your current Terminal session and open a new one, then run command printenv. You should then see your variable listed.
You can also unload it from the system by either running launchctl unload ~/Library/LaunchAgents/EnterNameOfYourFileHere.plist or removing it from that directory and restarting your computer.
  1 个评论
Walter Roberson
Walter Roberson 2018-5-2
Abdul Majeed Mohammed: is it DYLD_LIBRARY_PATH that you are trying to set? The rules about how that is used changed, but the way of setting environment variables did not change.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by