Environment variable

from Wikipedia, the free encyclopedia

As environment variable is called configurable variables in operating systems , often paths to specific programs or data included, as well as certain data and settings that can be used by multiple programs. Usually these are strings .

Another name is global variable ; however, this is rather unusual as this is used with a different meaning in many programming languages .

Users or applications can read and / or change values ​​of these variables.

Unix and Unix-like operating systems

Command line

In many UNIX- based operating systems, such as macOS , Linux or BSD , environment variables are set when a command line interpreter ( shell ) is started. Predefined environment variables are usually set automatically when a shell is started according to the entries in one or more configuration files. In the Bourne Shell , for example, this is the file /etc/profile. In addition, the shell uses a file (e.g. .profileor .cshrc) in the user's home directory that contains user-specific environment variables and can be adjusted by the user himself. These predefined environment variables can also be displayed by entering printenvin the shell. The program envdisplays all environment variables or lets a program run in a changed environment. Both commands are part of the coreutils .

Predefined environment variables

Some environment variables can be found on almost all Unix systems. Examples for this are:

HOME The path of the current user's personal directory
LOGNAME The name of the current user
MAIL The path in which the current user's personal e-mail messages are placed
PATH This variable contains the search path. If no directory is specified when entering a command , the shell searches the paths stored in this variable from left to right. The directory names are separated by a colon ":" under Unix operating systems. The current directory will not be searched as this is a security risk.

Change of environment variables

Environment variables can be set and made known to the other processes within the operating system as follows:

For Bourne, bash and shells based on them:

Setting the variable: <Variablenname>=<Variableninhalt>
Make the variable known: export <Variablenname>
Deleting the variable: unset <Variablenname>

For csh, tcsh and shells based on them:

Setting and making the variable known: setenv <Variablenname> <Variableninhalt>

View environment variables

View all environment variables

Query the variable: env

View a specific environment variable

For all shells:

Query the variable: echo $<Variablenname>

Process dependency

It is common for the environment variables to be saved per system process. Changes that a process makes to the variables are only valid for itself and for processes that it starts after the change (child processes).

DOS and Windows

The functionality under MS-DOS (and compatible DOS ) and Windows is basically the same, whereby variables under Windows can be set system -wide either in a Windows command prompt per session or in the control panel. Other functions and standard variables were also added.

Put

Environment variables can be set from the command line or from batch files ( .cmd, .BAT) with the SETcommand (for example SET PROMPT=$P$G). If the SETcommand is given alone, all environment variables are displayed, and if only the name of a variable is given, its value is displayed. You can delete them by entering the variable name followed by =(e.g. SET PROMPT=).

query

.BATEnvironment variables can be used in batch files ( ) by enclosing them in percent signs (e.g. CD %VARIABLE%). The called program then receives the command line with a substituted variable.

Furthermore, each program receives a copy of the environment variables of the calling program (with normal calls from COMMAND.COM ) in its environment segment (to which a pointer in the PSP at address 2C h refers). The environment segment of a program is discarded after it has ended.

Putting out of programs

The mechanism of the environment variables does not provide that the environment variables of the superordinate shell can be changed from a program or a nested shell (cmd) environment. Due to the inheritance of variables, changes to the own environment do not affect the environment of COMMAND.COM ( Master Environment ). Calling the command line command SET from within a program also has no effect, as this starts a new instance of COMMAND.COM.

The following trick was possible under DOS and older Windows versions: The environment of COMMAND.COM could be changed by finding the first memory control block via INT  21 AH=52hat address [BX-2h], and then searching memory control block by memory control block until the environment of COMMAND.COM finds and changes it. However, this no longer works with Windows NT, since there the COMMAND.COM environment is regularly reloaded by the NT kernel.

The definition of an environment variable in a program and subsequent use in a shell or batch script is possible by starting the shell or batch script from this program after changing the own environment. Such a call is usually supported in programming languages ​​by calling the respective operating system function, in Java, for example, with the java.lang.ProcessBuilder class. The changed environment of the program then acts as a parent for this call.

In Windows batch programs and under PC-compatible DOS, changing the environment of a called batch ( callcommand) affects the calling level because the COMMAND environment is not exited. This does not apply to calling a Unix shell script from within a script. The called script is embedded in a new (child) environment, which does not apply to the calling level.

In Windows it is now possible to influence the environment from a program: The program must create a small batch file in a temp directory or the current directory with the following content:

set VARIABLE=WERT

This temporary batch file is then called in the overall batch:

mysetenv.exe someparameters >setenv.bat
call setenv.bat
folgende batch-befehle

Windows command line

To display the value of an environment variable in the command line, use echo %NAME%or set NAME, where NAMEthe variable name is used. The setcommand is also available as a command line editor. It can also be used very well within batch files. The command setas such lists all set and thus available environment variables. If you want to create an environment variable or assign a new value to an existing one, you use it set NAME=WERT, whereby NAMEthe name and WERTthe future value of the variable are used instead . set /?provides detailed information on the functionality of the command.

Graphical user interface

The command rundll32 sysdm.cpl,EditEnvironmentVariablesor menu item “Control Panel - System” or the key combination Windows+ offers a graphical editor for direct editing of the environment variables Pause. The editor can be found under the “Advanced” tab and there under “Environment Variables”.

User variables and system variables

Newer Windows versions (for example Windows XP or Windows 7) differentiate between “user variables”, which only refer to the currently logged in user, and “system variables”, which are valid for the entire system (including all other user accounts).

Dynamic environment variables

Some DOS and Windows programs provide dynamically generated environment variables on their own. These are not permanently stored and the value is determined shortly before output. Examples of such dynamic environment variables are:

CD Specifies the directory in which the inquirer is currently located. (also DOS; classic: without drive letter; from Windows XP: with drive letter)
DATE Current system date
TIME Current system time
ERRORLEVEL Error code of the last command executed (also DOS)
RANDOM Generates a random number between 0 and 32767
CMDCMDLINE Contains the command line of the active command interpreter, see also COMSPEC

Predefined environment variables

Depending on the Windows version used, additional environment variables defined at system start or at login are available. The values ​​can depend on the national language and on the drive letter of the operating system drive. For example:

variable Windows XP Windows Vista / 7/8/10 comment
% ALLUSERSPROFILE% (% PROGRAMDATA%) C: \ Documents and Settings \ All Users C: \ ProgramData
% APPDATA% C: \ Documents and Settings \ {username} \ Application Data C: \ Users \ {username} \ AppData \ Roaming
% COMPUTERNAME% {computer name}
% COMMONPROGRAMFILES% C: \ Program Files \ Common Files C: \ Program Files \ Common Files
% COMMONPROGRAMFILES (x86)% C: \ Program Files (x86) \ Common Files C: \ Program Files (x86) \ Common Files
% COMSPEC% % SystemRoot% \ System32 \ cmd.exe The path to the command line interpreter
% SystemRoot% is also expanded accordingly
by DOS
% HOMEDRIVE% C: C:
% HOMEPATH% \ Documents and Settings \ {username} \ Users \ {username}
% LOCALAPPDATA% n / A C: \ Users \ {username} \ AppData \ Local
% LOGONSERVER% \\ {domain_logon_server}
% OS% Windows_NT Windows_NT
% PATH% % SystemRoot% \ system32;% SystemRoot%;% SystemRoot% \ System32 \ Wbem PATH
% SystemRoot% accordingly also expands
DOS
% PATHEXT% .COM, .EXE, .BAT, .CMD, .VBS, .VBE, .JS, .WSF, .WSH .com, .exe, .bat, .cmd, .vbs, .vbe, .js, .jse, .wsf, .wsh, .msc
% PROGRAMFILES% C: \ Programs C: \ Program Files
% PROGRAMFILES (X86)% C: \ Program Files (x86) C: \ Program Files (x86)
%PROMPT% $ P $ G also DOS
% SystemDrive% C: C:
% SystemRoot% C: \ Windows
(Windows 2000 and before: C: \ WINNT)
C: \ Windows
% TEMP% or% TMP% C: \ Documents and Settings \ {username} \ Local Settings \ Temp C: \ Users \ {username} \ AppData \ Local \ Temp TEMP
% USERDOMAIN% {userdomain}
% USERNAME% {username} The name of the current user
% USERPROFILE% % SystemDrive% \ Documents and Settings \ {username} C: \ Users \ {username}
% WINDIR% C: \ Windows C: \ Windows Historical form (16-bit Windows), retained for compatibility. Must always be identical to% SystemRoot%.
% PUBLIC% n / A C: \ Users \ Public
% PSModulePath% n / A % SystemRoot% \ system32 \ WindowsPowerShell \ v1.0 \ Modules \
PATHPaths separated by semicolons in which executable programs are to be searched for in this order if a command could not be found in the current directory. There is also another mechanism for program registration.
TEMPFor data protection, this should not be a general directory for all users, if possible, but a specific user directory; about% HOMEDRIVE %% HOMEPATH% \ TEMP.

Registration

Environment variables are stored in the registry under the newer NT based versions of Windows (Windows NT / 2000 / XP / 2003 / Vista). The registry path

 HKEY_CURRENT_USER\Environment

or with Windows XP (for e.g. HOMEDRIVE) and Windows Vista

 HKEY_CURRENT_USER\Volatile Environment

is used for environment variables that only affect the current user. The path

 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

on the other hand, it is used to store environment variables that are valid in the overall system. With the search function of the registry editor, the paths or keys can be searched for the desired entries.

The editor can be started with the command regeditin the run dialog ( Windows+ R).

Warning: Improper changes to the registry can have serious consequences . Changes should only be made if you know what you are doing. Before making any changes, you should make a backup of the relevant registration data!

Delay in applying changed variables

When a process (EXE file) is started, the current environment variables are read from the registry and then “frozen”, so the process receives a copy or a “snapshot” of the current variables when it is created. Environment variables that are changed later do not have any effect on the running processes (as long as the program ignores the corresponding Windows message), so that processes such as a running shell (“command prompt”) do not notice anything. If the environment variables are to be used in a shell, a new instance of the shell must be loaded. It does not matter whether an already open shell with the old environment variables was closed beforehand or not. This means that two shells can be run with different environment variables.

If the shell is started from a Windows Explorer (e.g. using “Open Command Window here”) and the option “Start folder window in its own process” is activated in the Windows Explorer setting, Windows -Explorer must be restarted so that the changed settings become visible.

Individual evidence

  1. Explanations of environment variables under Ubuntu Linux
  2. WM_SETTINGCHANGE message