Environment variable

An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.
They were introduced in their modern form in 1979 with Version 7 Unix, so are included in all Unix operating system flavors and variants from that point onward including Linux and macOS. From PC DOS 2.0 in 1982, all succeeding Microsoft operating systems, including Microsoft Windows, and OS/2 also have included them as a feature, although with somewhat different syntax, usage and standard variable names.
Design.
In all Unix and Unix-like systems, each process has its own separate set of environment variables. By default, when a process is created, it inherits a duplicate run-time environment of its parent process, except for explicit changes made by the parent when it creates the child. At the API level, these changes must be done between running codice_1 and codice_2. Alternatively, from command shells such as bash, a user can change environment variables for a particular command invocation by indirectly invoking it via codice_3 or using the codice_4 notation. A running program can access the values of environment variables for configuration purposes.
Shell scripts and batch files use environment variables to communicate data and preferences to child processes. They can also be used to store temporary values for reference later in a shell script. However, in Unix, not exported variables are preferred for this as they don't leak outside the process.
In Unix, an environment variable that is changed in a script or compiled program will only affect that process and possibly child processes. The parent process and any unrelated processes will not be affected. Similarly, changing or removing a variable's value inside a DOS batch file will change the variable for the duration of codice_5's existence.
In Unix, the environment variables are normally initialized during system startup by the system init startup scripts, and hence inherited by all other processes in the system. Users can, and often do, augment them in the profile script for the command shell they are using. In Microsoft Windows, each environment variable's default value is stored in the Windows registry or set in the codice_6 file.
On Unix, a setuid program is given an environment chosen by its caller, but it runs with different authority from its caller. The dynamic linker will usually load code from locations specified by the environment variables codice_7 and codice_8 and run it with the process's authority. If a setuid program did this, it would be insecure, because its caller could get it to run arbitrary code and hence misuse its authority. For this reason, libc unsets these environment variables at startup in a setuid process. setuid programs usually unset unknown environment variables and check others or set them to reasonable values.
Syntax.
The variables can be used both in scripts and on the command line. They are usually referenced by putting special symbols in front of or around the variable name. For instance, to display the user home directory, in most scripting environments, the user has to type:
echo $HOME
In DOS, OS/2 and Windows command-line interpreters such as codice_5 and codice_10, the user has to type this:
ECHO %HOME%
In Windows PowerShell, the user has to type this:
Write-Output $env:HOMEPATH
The commands codice_3, codice_12, and codice_13 display all environment variables and their values. codice_13 can also be used to print a single variable by giving that variable name as the sole argument to the command.
Assignment.
The commands codice_3 and codice_12 are also used to set environment variables and are often incorporated directly into the shell.
Unix.
In Unix, the following commands can also be used, but are often dependent on a certain shell.
 "VARIABLE"="value" #
 export "VARIABLE" # for Bourne and related shells
 export "VARIABLE"="value" # for ksh, bash, and related shells
 setenv "VARIABLE" "value" # for csh and related shells
A few simple principles govern how environment variables achieve their effect.
Environment variables are local to the process in which they were set. If two shell processes are spawned and the value of an environment variable is changed in one, that change will not be seen by the other.
When a child process is created, it inherits all the environment variables and their values from the parent process. Usually, when a program calls another program, it first creates a child process by forking, then the child adjusts the environment as needed and lastly the child replaces itself with the program to be called. This procedure gives the calling program control over the environment of the called program.
In Unix and Unix-like systems, the names of environment variables are case-sensitive.
In Unix shells, variables may be assigned without the codice_17 keyword. Variables defined in this way are displayed by the codice_12 command, but are not true environment variables, as they are stored only by the shell and are unknown to all other processes. The codice_13 command will not display them, and child processes do not inherit them.
 "VARIABLE"="value"
The prefix syntax exports a "true" environment variable to a child process without affecting the current process:
 "VARIABLE"="value" program_name [arguments]
The persistence of an environment variable can be session-wide or system-wide.
unset is a builtin command implemented by both the Bourne shell family (sh, ksh, bash, etc.) and the C shell family (csh, tcsh, etc.) of Unix command line shells. It unsets a shell variable, removing it from memory and the shell's exported environment. It is implemented as a shell builtin, because it directly manipulates the internals of the shell. Read-only shell variables cannot be unset. If one tries to unset a read-only variable, the unset command will print an error message and return a non-zero exit code.
DOS, OS/2 and Windows.
In DOS, OS/2 and Windows command-line interpreters such as codice_5 and codice_10, the codice_22 command is used to assign environment variables and values using the following arguments:
 SET "VARIABLE"="value"
The codice_22 command without any arguments displays all environment variables along with their values.
Examples.
Examples of environment variables include:
True environment variables.
DOS.
Under DOS, the "master environment" is provided by the primary command processor, which inherits the "pre-environment" defined in codice_66 when first loaded. Its size can be configured through the codice_67 parameter between 160 and 32767 bytes. "Local environment" segments inherited to child processes are typically reduced down to the size of the contents they hold. Some command-line processors (like 4DOS) allow to define a minimum amount of free environment space that will be available when launching secondary shells. While the content of environment variables remains unchanged upon storage, their names (without the "codice_68") are always converted to uppercase, with the exception of "pre-environment variables" defined via the codice_66 directive codice_22 under DR DOS 6.0 and higher (and only with codice_71 (for "allow lowercase names") under DR-DOS 7.02 and higher). In principle, MS-DOS 7.0 and higher also supports lowercase variable names (codice_72), but provides no means for the user to define them. Environment variable names containing lowercase letters are stored in the environment just like normal environment variables, but remain invisible to most DOS software, since they are written to expect uppercase variables only. Some command processors limit the maximum length of a variable name to 80 characters. While principally only limited by the size of the "environment segment", some DOS and 16-bit Windows programs do not expect the contents of environment variables to exceed 128 characters. DR-DOS codice_5 supports environment variables up to 255, 4DOS even up to 512 characters. Since codice_5 can be configured (via codice_75) to support command lines up to 1024 characters internally under MS-DOS 7.0 and higher, environment variables should be expected to contain at least 1024 characters as well. In some versions of DR-DOS, the environment passed to drivers, which often do not need their environment after installation, can be shrunken or relocated through codice_76 or codice_77/codice_78 options codice_79 (zero environment), codice_80 (substitute drive, e.g. codice_81) and codice_82 (relocate environment above program) in order to minimize the driver's effectively resulting resident memory footprint.
In batch mode, non-existent environment variables are replaced by a zero-length string.
"Standard environment variables" or "reserved environment variables" include:
The DR-DOS family supports a number of additional "standard environment variables" including:
Datalight ROM-DOS supports a number of additional "standard environment variables" as well including:
codice_366, codice_367, codice_368, codice_369, codice_370 and codice_371 are also used by ROM-DOS.
Windows.
"System path variables" refer to locations of critical operating system resources, and as such generally are not user-dependent.
"User management variables" store information related to resources and settings owned by various user profiles within the system. As a general rule, these variables do not refer to critical system resources or locations that are necessary for the OS to run.
"Optional System variables" are not explicitly specified by default but can be used to modify the default behavior of certain built-in console commands. These variables also do not need to be explicitly specified as command line arguments.
Default values.
The following tables shows typical default values of certain environment variables under English versions of Windows as they can be retrieved under codice_429.
In this list, there is no environment variable that refers to the location of the user's "My Documents" directory, so there is no standard method for setting a program's home directory to be the "My Documents" directory.
Pseudo-environment variables.
DOS.
Besides true environment variables, which are statically stored in the environment until changed or deleted, a number of pseudo-environment variables exist for batch processing.
The so-called "replacement parameters" or "replaceable parameters" (Microsoft / IBM terminology) aka "replacement variables" (Digital Research / Novell / Caldera terminology) or "batch file parameters" (JP Software terminology) codice_434..codice_435 and codice_436 can be used to retrieve the calling parameters of a batchjob, see codice_437. In batchjobs, they can be retrieved just like environment variables, but are not actually stored in the environment.
Some command-line processors (like DR-DOS codice_5, Multiuser DOS codice_439/codice_440 (Terminal Message Process), JP Software 4DOS, 4OS2, 4NT, Take Command and Windows cmd.exe) support a type of pseudo-environment variables named "system information variables" (Novell / Caldera terminology) or "internal variables" (JP Software terminology), which can be used to retrieve various possibly dynamic, but read-only information about the running system in batch jobs. The returned values represent the status of the system in the moment these variables are queried; that is, reading them multiple times in a row may return different values even within the same command; querying them has no direct effect on the system. Since they are not stored in the environment, they are not listed by SET and do not exist for external programs to retrieve. If a true environment variable of the same name is defined, it takes precedence over the corresponding variable until the environment variable is deleted again. They are not case-sensitive.
While almost all such variables are prefixed with an underscore ("codice_441") by 4DOS etc. by convention (f.e. codice_442), they are not under DR-DOS codice_5 (f.e. codice_262).
In addition, 4DOS, 4OS2, 4NT, and Take Command also support so called "variable functions", including user-definable ones. They work just like "internal variables", but can take optional parameters (f.e. codice_445) and may even change the system status depending on their function.
"System information variables" supported by DR-DOS codice_5:
"System information variables" supported by DR-DOS codice_5 with networking loaded:
Windows.
"Dynamic environment variables" (also named "internal variables" or "system information variables" under DOS) are pseudo-environment variables supported by codice_429 when command-line extensions are enabled, and they expand to various discrete values whenever queried, that is, their values can change when queried multiple times even within the same command. While they can be used in batch jobs and at the prompt, they are not stored in the environment. Consequently, they are neither listed by codice_22 nor do they exist for external programs to read. They are not case-sensitive.
Indirectly, they are also supported under Windows' codice_5, which has been modified to internally call codice_604 to execute the commands.