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.
From the System Properties screen click on the Advanced tab where you will see the Environment Variables button. Click on the Environment Variables button.
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.
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.
The newly added variable will be listed in the related User or System variables pane like below.
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 variables. First, open the MS-DOS which is explained below.
We will use the echo command which will print the 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 and all of them have environment variables. The environment variables are related to the shell for Linux. The bash shell is the default shell for most 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 the 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"
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.
We can list newly added and existing variables with the
env command. This can be useful to check a variable existing to prevent errors and misconfigurations.
If we do not want to list all variables and only check our newly added variable we can use the
grep command to filter env output. In the following example, we only list specific variables.
$ env | grep myvar
myvar=This is my variable
Set Environment Variable In MacOSX
The MacOSX operating system is a variant of the Unix whereas Linux is also a variant of the Unix. The instructions used in Linux can be used for MacOSX too.