Environment variables

Environment variables

Our video

Loading your "FAQ: Environment variables" video

Environment variables

A environment variable is a dynamic value, loaded into memory, which can be used by several processes running simultaneously. On most operating systems, the locations of certain libraries, or even of the main system executables, may have a different location depending on the installation.




Thus, thanks to the environment variables, it is possible, from a program, to refer to a location by relying on the environment variables defining this data.

Windows

On Windows, environment variables are surrounded by the "%" character. So, to display the value of an environment variable, all you have to do is type a command like:

echo% NOM_DE_LA_VARIABLE%

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

Environment variable Description
% APPDATA% Returns a path to the default directory containing user programs.
%CMDCMDLINE% Returns the exact command used to access the command interpreter (cmd.exe).
%COMPUTERNAME% Returns the name assigned to the computer.
%DATE% Returns the current date.
% ERRORLEVEL% Contains the error code of the last command used.
%HOMEDRIVE% Contains the drive letter where the current user's directory is located.
%HOMEPATH% Contains the full path to the current user's directory.
%NUMBER_OF_PROCESSOR% Contains the number of processors present on the system.
%YOU% Contains a character string describing the installed operating system. Note that Windows 2000 declares itself to be WINDOWS_NT.
% PATH% Specifies the path to the main system executables.
%PATHEXT% Describes the extensions that the system considers 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 drive letter the system is located on (usually C :).
% SYSTEMROOT% Contains the path to the system root directory.
% TEMP% Contains the path of the temporary directory for applications.
%TMP% Contains the path of the temporary directory for applications.
%TIME% Returns the current time.
%USERDOMAIN% Contains the domain to which the current account belongs.
% USERNAME% Contains the username corresponding to the current account.
% USERPROFILE% Contains the location of the current account user profile.
%WINDIR% Contains the path to the system directory (usually C: WINDOWS or C: WINNT).

On Windows, the set command is used to create, modify, and display environment variables. However, the changes are made only in the current session.




To display a variable, the command is as follows:


set NomVariable

To create a variable, the command is as follows:

set NomVariable=valeur

To delete a variable, the command is as follows:

set NomVariable=

You can use any character as a value, including spaces. However, to be able to use special characters (<,>, |, & or ^), it suffices to precede them with the escape character (^) or to surround them with quotes, which will then be part of the value because all characters after the = sign are taken into account. For example, to define "black & white" as the value:


set color = black ^ & white
ou
set varname="new&name"

Under UNIX

On UNIX systems, environment variables are preceded by the "$" character and surrounded by braces, although braces are not required. Given the variety of UNIX-like systems (in particular the different distributions of Linux), environment variables Thus, to display the value of an environment variable, just 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 architecture of the machine.
$DISPLAY Contains the identifier of the display terminal to be used in the window manager (X11).
$ HOME Returns the path to the directory of the current user.
$HOST Returns the name of the computer.
$ LANG Returns the default language code.
$ PATH Returns a list of paths to directories containing executables, separated by semicolons.
$PRINTER Contains the name of the default printer.
$SHELL Specifies the path of the command interpreter used.
$ USER Returns the identifier of the current user.
add a comment of Environment variables
Comment sent successfully! We will review it in the next few hours.