Q100015: How to set environment variables

Follow

SUMMARY

This article explains how to set environment variables for supported versions of Windows, macOS and Linux, which can be done either permanently or temporarily. Environment variables are dynamic-named values which can be used to alter the behaviour of a process that uses them.


MORE INFORMATION

Setting environment variables permanently

Setting environment variables permanently ensures that the variable will be available whenever a process calls it, even after computer restarts and regardless of how the process is launched. Below are instructions for how to permanently set environment variables on each OS:

Windows
  1. Type "env" in the Windows Taskbar Search and select Edit the system environment variables.

    Screen_Shot_2021-12-09_at_1.43.35_PM.png

  2. Click the Environment Variables... button.

    Screen_Shot_2021-12-09_at_1.45.30_PM.png

  3. Click New to create a new environment variable.
    Alternatively, click Edit to modify an existing environment variable.

    Screen_Shot_2021-12-09_at_1.48.43_PM.png

    NOTE: Depending on whether you want to set the variable for the current user or all users, utilise the buttons in User variables or in System variables section, respectively. Setting environment variables for all users requires administrator privileges.

  4. In the Variable name field, enter the name of the environment variable you want to set.

  5. In the Variable value field, enter the value for the variable. The value can be a directory path, for example.

    Screen_Shot_2021-12-09_at_1.50.19_PM.png

  6. Click OK to have the change take effect.
    Close all remaining windows by clicking OK.

NOTE: When editing existing system variables, or adding or deleting either user or system variables, you may need to log each user off and on again before your changes to environment variables take effect.

 

macOS
  1. Open TextEdit application (Type TextEdit in Spotlight Search [Command]+[Spacebar]).

    Screen_Shot_2021-12-09_at_11.32.55_AM.png

  2. In TextEdit, use the Make Plain Text command in the Format menu (or [Shift]+[Command]+[T]).

    Screen_Shot_2021-12-09_at_11.35.25_AM.png

  3. Copy and Paste the following (or download the file attached at the end of this article):

    <?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.environment</string>
    <key>ProgramArguments</key>
    <array>
        <string>sh</string>
        <string>-c</string>
        <string>
        launchctl setenv MY_VARIABLE my_value
        launchctl setenv TEST_VARIABLE test_value
        </string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    </dict>
    </plist>

  4. Change “MY_VARIABLE” and “MY_VALUE” to your variable and value, respectively. Remove the TEST_VARIABLE line if you don’t need to set multiple environment variables.
    NOTE: The label, my.environment, needs to be a unique name from other .plist files

  5. Open the Save dialog in the File menu ([Command]+[S]):

  6. Open Go to the folder with [Command]+[Shift]+[G] and type ~/Library/LaunchAgents

    Screen_Shot_2021-12-09_at_12.47.56_PM.png

  7. Save the file as my.environment.plist. Make sure to use .plist
    NOTE: The filename should be the same as the label.

    Screen_Shot_2021-12-09_at_12.49.33_PM.png

  8. Restart the computer to apply the environment variables.
    Make sure to unselect Reopen windows when logging back in.

    Screen_Shot_2021-12-09_at_12.50.38_PM.png

    Alternatively, if you don’t want to restart, and apply the environment variables immediately, run the following command in the Terminal app:
    launchctl load ~/Library/LaunchAgents/my.environment.plist
    Then completely close out of the Terminal app.

    Screen_Shot_2021-12-09_at_1.18.35_PM.png

 

You can verify the environment variable has been set by running the following command in the Terminal app:

export

Or you can run the following in Nuke's Script Editor:

import os
print(os.environ)

 

Linux
  1. The procedure for setting an environment variable depends on your system's default shell. Open a Terminal window and enter the following to get the name of your shell:

    echo "$SHELL"
     
  2. Depending on the output of the previous step, do one of the following:
    • If your shell is a csh or tcsh shell, add the following command to the .cshrc or .tcshrc file in your home directory: setenv VARIABLE value. Replace VARIABLE with the name of the environment variable and value with the value you want to give it, for example:
      setenv NUKE_PATH /SharedDisk/Nuke

    • If your shell is a bash or ksh shell, add the following command to the .bashrc or .kshrc file in your home directory: export VARIABLE=value. Replace VARIABLE with the name of the environment variable and value with the value you want to give it, for example:
      export NUKE_PATH=/SharedDisk/Nuke

 

 

Setting environment variables temporarily

Setting environment variables temporarily allows you to troubleshoot by changing a particular environment variable just for that Command Prompt/Terminal session. Other sessions on the same system will not use the environment variables you set in this way. Below are instructions for how to temporarily set environment variables for each platform:

Windows
  1. Launch the Command Prompt app.

  2. Define the environment variable by executing the set command using the format set VARIABLE=value, for example:
    set NUKE_PATH=N:\SharedDisk\Nuke

  3. Launch your application from the same Command Prompt session for it to use that environment variable.

 

macOS
  1. Launch the Terminal app.

  2. Define the environment variable by executing the export command using the format export VARIABLE=value, for example:
    export NUKE_PATH=/SharedDisk/Nuke

  3. Launch your application from the same Terminal session for it to use that environment variable.

 

Linux
  1. The procedure for setting an environment variable depends on your system's default shell. Open a Terminal window and enter the following to get the name of your shell:

    echo "$SHELL"
     
  2. Depending on the output of the previous step, do one of the following:
    • If your shell is a csh or tcsh shell, then define the environment variable by executing the setenv command using the format setenv VARIABLE value, for example:
      setenv NUKE_PATH /SharedDisk/Nuke

    • If your shell is a bash or ksh shell, then define the environment variable by executing the export command using the format export VARIABLE=value, for example:
      export NUKE_PATH=/SharedDisk/Nuke

  3. Launch your application from the same Terminal session for it to use that environment variable.

 

 

 

FURTHER READING

More information about handling environment variables can be found here in the following articles:

Q100017: How to list environment variables

Q100127: How to delete or unset environment variables

Q100679: How to get environment variable values

 

We're sorry to hear that

Please tell us why