Environment variable
An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables 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, as well as on Windows, 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 runningfork and exec. Alternatively, from command shells such as bash, a user can change environment variables for a particular command invocation by indirectly invoking it via env or using the ENVIRONMENT_VARIABLE=VALUE <command> 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, [|non-exported variables] are preferred for this as they do not 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 or Windows batch file will change the variable for the duration of
COMMAND.COMor CMD.EXE's existence, respectively.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
AUTOEXEC.BAT 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
$LD_LIBRARY_PATH and $LD_PRELOAD 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.In general, the collection of environment variables function as an associative array where both the keys and values are strings. The interpretation of characters in either string differs among systems. When data structures such as lists need to be represented, it is common to use a colon or semicolon-delineated list.
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.By convention, names of environment variables are normally expressed in all capital letters. This helps keep environment variables distinctly different from other variables and identifiers used in programming codes. Nevertheless, note that case sensitivity in environment variable names differs between operating systems. That is, Unix-like operating systems are case-sensitive with respect to environment variable names, while DOS, OS/2, and Windows are not case-sensitive.
Unix
In most Unix and Unix-like command-line shells, an environment variable's value is retrieved by placing a$ sign before the variable's name. If necessary, the name can also be surrounded by braces.To display the user home directory, the user may type:
echo $HOME
In Unix and Unix-like systems, the names of environment variables are case-sensitive.
The command
env displays all environment variables and their values. The command printenv can also be used to print a single variable by giving that variable name as the sole argument to the command.DOS, OS/2 and Windows
In DOS, OS/2 and Windows command-line interpreters such asCOMMAND.COM and CMD.EXE, an environment variable is retrieved by placing a % sign before and after it.In DOS, OS/2 and Windows command-line interpreters as well as their API, upper or lower case is not distinguished for environment variable names.
The environment variable named
HOMEDRIVE contains the drive letter of the user's home directory, whilst HOMEPATH contains the full path of the user's home directory within that drive.So to see the home drive and path, the user may type this:
ECHO %HOMEDRIVE%%HOMEPATH%
The command
SET displays all environment variables and their values. In Windows NT and later set can also be used to print all variables whose name begins with a given prefix by giving the prefix as the sole argument to the command.In Windows PowerShell, the user may type the following:
"$Env:HomeDrive$Env:HomePath"
or one of the following redundant equivalents:
Write-Output "$Env:HomeDrive$Env:HomePath"
echo "$Env:HomeDrive$Env:HomePath"
write "$Env:HomeDrive$Env:HomePath"
return "$Env:HomeDrive$Env:HomePath"
Write-Host "$Env:HomeDrive$Env:HomePath"
In PowerShell, upper or lower case is not distinguished for environment variable names.
The following command displays all environment variables and their values:
Get-ChildItem env:
Assignment: Unix
The commandsenv and set can be used to set environment variables and are often incorporated directly into the shell.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 shells, variables may be assigned without the
export keyword. Variables defined in this way are displayed by the set command, but are not true environment variables, as they are stored only by the shell and are unknown to all other processes. The printenv 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
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 and the C shell family 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.Assignment: DOS, OS/2 and Windows
In DOS, OS/2 and Windows command-line interpreters such asCOMMAND.COM and CMD.EXE, the SET command is used to assign environment variables and values using the following arguments:SET VARIABLE=value
An environment variable is removed via:
SET VARIABLE=
The
SET command without any arguments displays all environment variables along with their values; SET " ", zero or more spaces, will include internal variables too. In CMD.EXE, it is possible to assign local variables that will not be global using the SETLOCAL command and ENDLOCAL to restore the environment.Use the switch
/? to display the internal documentation, or use the viewer help:SET /?
HELP SET
SETLOCAL /?
HELP SETLOCAL
In PowerShell, the assignment follows a syntax similar to Unix:
$env:VARIABLE = "VALUE"