Environment variables

May 2015

Environment variables

An environment variable is a dynamic value loaded into the memory that can be used by several processes operating simultaneously. On most operating systems, the location of some libraries or of the main system executables may be different depending on the installation.

It is therefore possible, from a given programme, to refer to a location based on the environment variables that define these data.

In Windows

In Windows, the environment variables are placed between the "%" character. Thus to display the value of an environment variable, simply type following type of command:

echo %NAME_OF_THE_VARIABLE%

Here is a non-exhaustive list of the main environment variables on Windows systems:

Environment variable Description
%APPDATA% Returns an access path to the default directory containing the user programmes (by default: C:\Program Files\.
%CMDCMDLINE% Returns the exact command used to access the command interpreter (cmd.exe).
%COMPUTERNAME% Returns the name attributed to the computer.
%DATE% Returns the day's date.
%ERRORLEVEL% Contains the error code of the latest command used.
%HOMEDRIVE% Contains the letter of the drive on which the current user's directory is located.
%HOMEDRIVE% Contains the letter of the drive on which the current user's directory is located.
%HOMEPATH% Contains the full access path to the current user's directory.
%NUMBER_OF_PROCESSOR% Contains the number of processors present on the system.
%OS% Contains a chain of characters describing the operating system installed. Note that Windows 2000 is identified as WINDOWS_NT.
%PATH% Specifies the access path to the system's main executables.
%PATHEXT% Describes the extensions that the system considers to be executable.
%PROCESSOR_ARCHITECTURE% Describes the architecture of the processor (x86 for Intel machines, IA64 for RISC architectures).
%RANDOM% Returns an integer between 0 and 32167 chosen at random by the system.
%SYSTEMDRIVE% Contains the letter of the drive on which the system is located (generally C:).
%SYSTEMROOT% Contains the access path to the system's root directory.
%TEMP% Contains the temporary directory access path for the applications.
%TMP% Contains the temporary directory access path for the applications.
%TIME% Returns the day's date.
%USERDOMAIN% Contains the domain to which the current account belongs.
%USERNAME% Contains the user name corresponding to the current account.
%USERPROFILE% Contains the location of the current account user profile.
%WINDIR% Contains the access path to the system's directory (generally C:\WINDOWS or C:\WINNT).

In Windows, the set command allows creation, modification and display of the environment variables. However, changes are made only in the session in progress.

To display a variable, the following command is used:

set VariableName 

To create a variable, the following command is used:

set VariableName=value

To delete a variable, the following command is used:

set VariableName=
It is possible to use any character as a value, including spaces. However, to be able to use special characters (<, >, |, & or ^), simply place the (^) escape character in front of them or quotation marks around them, which will then be part of the value since all characters following the = sign are taken into account. For example, to define "black&white" as the value:
set color=black^&white
	or
set varname="new&name"

In UNIX

In UNIX systems, the environment variables are preceded by the "$" character and placed between braces, even though the braces are not compulsory. Given the variety in UNIX systems (particularly the different Linux distributions), the environment variables Thus to display the value of an environment variable, simply type one of the following commands:

echo $VARIABLE
echo ${VARIABLE}

Here is a non-exhaustive list of the main environment variables on UNIX systems:

Environment variable Description
$ARCH Contains the description of the machine's architecture.
$DISPLAY Contains the ID of the display terminal to be used in the window administrator (X11).
$HOME Returns the access path to the current user's directory.
$HOST Returns the computer name.
$LANG Returns the default language code.
$PATH Returns a list of access paths to directories containing executables, separated by semi-colons.
$PRINTER Contains the name of the default printer.
$SHELL Indicates the path of the command interpreter used.
$USER Returns the ID of the current user.
For unlimited offline reading, you can download this article for free in PDF format:
Environment-variables .pdf

See also


Variables del entorno
Variables del entorno
Umgebungsvariablen
Umgebungsvariablen
Variables d'environnement
Variables d'environnement
Variabile d'ambiente
Variabile d'ambiente
Variáveis de ambiente
Variáveis de ambiente
This document entitled « Environment variables » from Kioskea (en.kioskea.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.