How do I set add or change environment variables?

289 views (last 30 days)
In the process of troubleshooting MATLAB, I have been asked to change the environment variables on my machine.  How can I set or change the environment variables?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Aug 2023
Edited: MathWorks Support Team on 18 Aug 2023
Below are instructions for setting environment variables on different Operating Systems.
Windows
Open the Environment Variables dialog by following these two steps:
1) In Windows Start Menu, type "env" then select "Edit the system environment variables"
2) Click the "Environment Variables..." button
You can create System Variables or User Variables using the "New..." button.
You can edit existing variables by selecting one from the list then click the "Edit..." button.
The system variables will be shared for all users, while user variables only affect your account/profile.
Linux/macOS
There are multiple ways to set environment variables depending on the type of shell you are using.  To determine the your shell type, use the "echo" command to see the value of the "SHELL" environment variable:
echo $SHELL
For a tcsh or csh shell, use the following syntax:
setenv <name> <value>
for example:
setenv JAVA_HOME /usr/local/
For a bash shell, use the following syntax:
export <name>=<value>
for example:
export JAVA_HOME=/usr/local/
​Note that setting an environment variable using these methods on Linux or macOS will only affect the current Terminal session. If you try to open a new shell or start a program outside your original shell the environment variable will not apply.
  1 Comment
Walter Roberson
Walter Roberson on 15 Jan 2023
The Windows sysdm.cpi that is discussed above makes permanent changes to the system.
For MacOS you should change a launchd plist https://apple.stackexchange.com/questions/289060/setting-variables-in-environment-plist and see https://stackoverflow.com/questions/135688/setting-environment-variables-on-os-x but be careful about which version the remarks there refer to (further down the page some people are specific about what is needed for which version.)

Sign in to comment.

More Answers (0)

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!