How To Set Environment Variables For Windows, Linux, and MacOSX?

The environment variables are used to store some data in different environments which can be an operating system, a programming language, a framework, etc. The most popular use case for environment variables is using operating systems like Windows, Linux, MacOSX, etc. These operating systems provide environment variables in order to provide different applications, commands, binaries, and tools for some variables or data in an easy way. In this tutorial, we will learn how to set an environment variable in Windows, Linux, and MacOSX. These instructions can be implemented in Windows XP, Windows 7, Windows 10, Windows Server, Linux, Ubuntu, Debian, Mint, Kali, CentOS, Fedora, RHEL, etc.

Set Environment Variable In Windows (Windows XP, Windows 7, Windows 10, Windows Server)

Windows operating system provides different ways to set environment variables. The most known way is to use the “Environment Variables” tool to set from GUI. In order to open the Environment Variables screen follow these steps.

Open the Windows Run by pressing WIN+R keys and then type sysdm.cpl and click OK.

Open System Properties From Windows Run

From the System Properties screen click on the Advanced tab where you will see the Environment Variables button. Click on the Environment Variables button.

Open Environment Variables From System Properties

The Environment Variables screen will be displayed like below. As we can see that there is two types of variables called User variables and System variables. User variables are only used by the current user. System variables are used by all users.

Environment Variables

We will click on the New button according to our case where for this example we will create a new user variable. The following New User Variable dialog box contains the Variable name and Variable value box where we will put this information. The last step is clicking on the OK button which will save and set these new variables.

New User Variable

The newly added variable will be listed in the related User or System variables pane like below.

List Environment Variables

Now we know that the environment variable is added but we can also use the command line interface to check the new environment variable. The command prompt or MS-DOS can be used to check new environment variable. First open the MS-DOS which is explained below.

We will use the echo command which will print given environment variable to the screen. The environment variable MYVAR can be accessed as %MYVAR% like below.

> echo %MYVAR%

Set Environment Variable In Linux (Ubuntu, Debian, Mint, Kali, CentOS, Fedora, RHEL)

Linux provides different distributions where all of them have the environment variables. The environment variables are related to the shell for Linux. The bash shell is the default shell for most of the Linux distributions. The create and set a new environment variable in Linux the bash can be used. The .bashrc is the configuration file that is used to set bash configuration and new variables. Every user has the .bashrc file in his home directory. The path is ~/.bashrc and the following line can be added to this file. This variable can be used by the current user.

export myvar="This is my variable"
Add Environment Variable Into .bashrc File

Now by opening a new shell or terminal we can use the newly added variable. The $ sign is used before the variable name which means in order to access myvar environment variable we will use $myvar.

echo $myvar

Set Environment Variable In MacOSX

The MacOSX operating system is a variant of the Unix where Linux is also a variant of the Unix. The instructions used in Linux can be used for MacOSX too.

Leave a Comment