What are Environment Variables?

Deepak Gupta January 10, 2022
Updated 2022/01/10 at 9:52 PM

Operating systems have many components and pieces of software responsible for keeping them running smoothly. Other of these components they direct the way that users and developers can interact with the system. There are components widely used by developers to establish default settings. These settings are often used to establish parameters for the execution of a specific program or application.

Among these parameters we have the Environment Variables. These variables are of great importance in the system since they can be defined or modified by the users or the system itself. In this way, a configuration necessary for the correct operation of a software or operating system would be prepared. If you want to know what system variables are and how they are used, I invite you to continue reading. We already started.

Table of Contents


Environment Variables

Environment variables are nothing more than variables whose value can change or be set within the environment of an operating system. These variables can affect the way certain processes are executed within the operating system.

These variables are part of the process execution environment. As an example, if a possessed needs to know the directory of temporary files in Windows, he only has to consult the value within the environment variables the value of % TEMP%.

Environment variables are included in all modern operating systems of type Unix. For its part, Microsoft systems have included this feature in all their systems Windows and OS / 2. Over time the syntax and names of the environment variables to be used and queried have changed.

How to get and modify environment variables

Environment variables can be accessed from a script or from the command line. This access can be only to consult as well as to modify its value. Each operating system implements the paths and syntax required for accessing and modifying environment variables.

For Unix operating systems, the way environment variables are accessed is in dependence of the shell used. This is because this tool is in charge of handling environment variables.

Windows system

Creating or modifying an environment variable in Windows is really easy. This process pIt can be done graphically or through cmd.

Using the user interface.

To access the environment variables we open a Windows explorer. We right click on “This Team” and select the properties option.

In the properties window we select the button “Environment Variables” and in this way we have access to the window to create / modify any environment variable. Once we have access to the environment variables window we can see that there are two groups of variables: user and system.

Environment variables window.

Actually, the two do not differ much, which is a more organizational way. The user variables you create They can only be accessed and modified by the user who creates them. At the system level, they can be used by any user and the operating system in general. The value assigned to the variable when it is created will be the default value that will have the same.

Create environment variable.

To edit a variable we look for it and select it in the list and go to the Edit option. Here you can modify the value and name of the variable. Note that if any program uses such a variable and changes the name, this program will throw an error.

To remove one of the environment variables, simplye select it from the list and choose the delete option.

Using the cmd

To access and see the value of an environment variable through the Windows command line, we first access the cmd. For this we write CMD in the search bar of the start menu. Another way is to press the key combination Windows + r, type cmd and then press Enter.

Once the console is open, we write the command echo% Var% where “Var” is the name of the variable and this shows us the value of said variable.

View with the Windows cmd.

If we want to change the value of the variable we use the SET command. This parameterless command displays all existing environment variables. If we write the command in the form SET VAR = VALUE, we will be assigning the “VALUE” in the value of the variable.

Changing value of a variable.

Unix / GNU / Linux systems

To display the value of an environment variable in systems Unix and Linux we can use specific interfaces like bash. We execute the echo command $ VAR where “VAR”Is the name of the variable and we will see its value.

For the treatment of variables, the commands are also used env, set, and printenv. To assign a value to an environment variable it is simply necessary to write Var_name = Value.

In these systems there are many ways to deal with environment variables. Creating and modifying them can be done in multiple ways depending on the command interpreters used.

Most common environment variables

There are many environment variables that are predefined by the system and are in common use. The value of these variables is practically the same between different operating systems and generally refer to directories, extensions and formats.

Windows system

% PATH%: This variable stores a list of the executable directories of the system applications. If we assign the value PATH =% PATH%; c: Program Files Mozilla Firefox we can open a URL with the Firefox browser directly from the CMD. We just have to write the Firefox command https://bigsoftware.es in a cmd, to cite an example.

% PATHEXT%: All executable file extensions are stored in this variable. In case the extension is in this variable, it is not necessary to write it to execute said executable. Among the most common values ​​we have .COM, .EXE, .BAT, .CMD, .JS, .JSE and others.

% TEMP% or% TMP%: The directory of temporary system files is stored here.

There are other types of variables that do not have a fixed value. These variables acquire their value once they are invoked. For example, if we invoke the% DATE% variable we will obtain the current system time. Among these we have % CD%,% TIME%,% RANDOM%,% ERRORLEVEL%,% CMDEXTVERSION%,% CMDCMDLINE%. I invite you to try them and to see the value that each one returns.

Unix systems

On unix systems we also have quite a few environment variables.

$ PATH: This is analogous to the Windows system. Contains the path by which the Shell will search for application executables.

$ HOME: Stores the address of the system user directory.

$ DISPLAY: Stores the identifier for the display.

$ SHELL: Displays the name of the Shell currently in use.

$ EDITOR: Path to the default text editor, for example Nano or gedit.

$ HOSTNAME: Stores the name for the system host.


Share this Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *