How to setup environment variables for Windows, Mac and Linux

engineering Mar 09, 2021

Intro

In this quick post, I'll cover how to setup environment variables for your Windows and Mac machines. For those Linux users out there, fortunately, it's pretty identical to setting them up using the Mac instructions below.

Setting up environment variables for Windows

  1. Launch Environment Variables in Windows.

    Click on the Windows icon start menu and start typing 'environment variables' in the search box.

    You should see search result such as "Edit the system variables". Go ahead and select this.

    environment variable result in window search

    You will be prompted with the "System Properties" dialog. Click on the "Environment Variables"

    system-properties-dialog
  2. Create a new Environment Variable

    In the Environment Variable dialog, click on New.

    environment-variables-settings

    A new New User Variable dialog should appear.

    new-user-env-dialog

    Go ahead and enter your desired environment variable.

    The example below is setting an environment variable called MY_VARIABLE_ID with a value of my_fancy_setting_value.

    new-user-env-dialog-example

    Click Ok to save.

Testing environment variables in Windows

To verify that the environment variables have been loaded and working correctly, we can carry out a quick test.

  1. Launch Windows Command Prompt.

    If you have one open already, be sure to close it down. Otherwise, it doesn't know to load the environment variables.

    Then, type the following to verify if the environment variable has been loaded correctly.

    echo %MY_VARIABLE_ID%
    

    For Powershell users

    $env:MY_VARIABLE_ID
    

    If loaded correctly, it will print out the corresponding environment variable.

Setting up environment variables For Mac and Linux users

To setup environment variables for Mac or Linux, we need to find out if your shell is running on zsh or bash.

Launch a terminal and type the command below to identify what shell you are running.

echo $SHELL

Editing your zshrc or bashrc file

Once you have identified the type of shell we shall update the corresponding rc file.

For those that are running zsh, we need to update the zshrc file. For those running on bash we need to update the bashrc file.

In your favourite editor, edit the relevant rc file like so.

For zshrc

nano ~/.zshrc

For bash

nano ~/.bashrc

Add your environment variable like so

export MY_VARIABLE_ID=my_fancy_setting_value

Save and close the file.

Testing environment variables on Mac or Linux

Launch a new terminal. Again, if you have one open already, be sure to close and re-open it. Otherwise, the newly saved environment variable will not be loaded.

echo $MY_VARIABLE_ID

For the fancy people using cmder in Windows

For those of you that use cmder, there is an alternative way.

In the folder location of where cmder is installed, there will be a folder called config.

You want to edit the config\user_profile.cmd file.

In the file, you want to add your environment variable like so

@set MY_VARIABLE_ID=my_fancy_setting_value

Be sure to save the file.

Testing environment variables in Cmder

Open a new terminal in Cmder and type the following command.

echo %MY_VARIABLE_ID%

This should print out the corresponding environment variable.

Tags