Environment variable

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.

Synopsis
In all Unix and Unix-like systems, each process has its own private set of environment variables. By default, when a process is created it inherits a duplicate environment of its parent process, except for explicit changes made by the parent when it creates the child. At API level, these changes must be done between fork and exec. Alternatively, from shells such as bash, you can change environment variables for a particular command invocation by indirectly invoking it via env or using the ENVIRONMENT_VARIABLE=VALUE &lt;command&gt; notation. All Unix operating system flavors as well as DOS and Microsoft Windows have environment variables; however, they do not all use the same variable names. Running programs can access the values of environment variables for configuration purposes. Examples of environment variables include:


 * PATH - lists directories the shell searches, for the commands the user may type without having to provide the full path.
 * HOME (Unix-like) and userprofile (Microsoft Windows) - indicate where a user's home directory is located in the file system.
 * TERM (Unix-like) - specifies the type of computer terminal or terminal emulator being used (e.g., vt100 or dumb).
 * PS1 (Unix-like) - specifies how the prompt is displayed in the Bourne shell and variants.
 * MAIL (Unix-like) - used to indicate where a user's mail is to be found.

Shell scripts and batch files use environment variables to store temporary values for reference later in the script, and also to communicate data and preferences to child processes.

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. In DOS changing a variable's value (or removing it) inside a BATCH file will change the variable for the duration of command.com's existence.

In Unix, the environment variables are normally initialized during system startup by the system init scripts, and hence inherited by all other processes in the system. Users can, and often do, augment them in the profile script for the shell they are using. In Microsoft Windows, environment variables defaults are stored in the windows registry or set in autoexec.bat.

Getting and setting environment variables
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 program search path, in most scripting environments, the user has to type: On DOS or Windows system, the user has to type this:

UNIX
The  ,  , and   commands display all environment variables and their values. and  are also used to set environment variables and are often incorporated directly into the shell. can also be used to print a single variable by giving that variable name as the sole argument to the command.

In UNIX, the following commands can also be used, but are often dependent on a certain shell. export VARIABLE=value # for Bourne, bash, and related shells setenv VARIABLE value # for csh and related shells

DOS and Windows
In DOS and Windows, the   command without any arguments displays all environment variables along with their values.

To set a variable to a particular value, use: However, this is temporary. Permanent change to the environment variable can be achieved through editing the registry (not recommended for novices) and using the Windows Resource Kit application. With the introduction of Windows Vista, the  command became part of Windows. The most common method of setting an environment variable in Windows is via .

To see the current value of a particular variable, use: or

Unexported variables
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 in the shell and not in the kernel. They will not be detected by the printenv command and are not inherited by child processes.

VARIABLE=value

However, if used in front of a program to run, the variables will appear as real environment variables to the program:

VARIABLE=value program_name [arguments]

There is no equivalent in the DOS or Windows environments.

Security
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.

Examples of UNIX environment variables
Contains a colon-separated list of directories that the shell searches for commands that do not contain a slash in their name (Commands with slashes are interpreted as a file name to execute, and the shell attempts to execute them as they are). See Path (computing)
 * $PATH

Contains the location of the user's home directory. Although the current user's home directory can also be found out through the C functions getpwuid and getuid</tt>, $HOME</tt> is often used for convenience in various shell scripts (and other contexts).
 * $HOME

This variable points to the current directory. Equivalent to the output of the command pwd when called without arguments.
 * $PWD

Contains the identifier for the display that X11 programs should use by default.
 * $DISPLAY

On many Unix systems with a dynamic linker, contains a colon-separated list of directories that the dynamic linker should search for shared objects when building a process image after exec</tt>, before searching in any other directories.
 * $LD_LIBRARY_PATH

LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is, then the language is set to (Brazilian) Portuguese and the locale  to Brazil. Refers to Time zone. It can take values like GMT, AST, etc.
 * $LANG, $LC_ALL
 * $TZ

Examples of DOS environment variables
This variable contains the full path to the command processor, command.com.
 * %COMSPEC%

This variable points to the current error level. If there was an error in the previous command, this is what you need to check against to find out about that.
 * %ERRORLEVEL%

This variable contains a semicolon-delimited list of directories in which the command interpreter will search for executable files. Equivalent to the UNIX $PATH variable (although note that PATH on Windows additionally performs the same task as LD_LIBRARY_PATH on Unix-like systems). Note that %PATH% can also be set like this PATH=c:\dos; where SET isn't required.
 * %PATH%

These variables contain the path to the directory where temporary files should be stored.
 * %TEMP% and %TMP%

Discrete value variables
These variables generally expand to discrete values, such as the current working directory, the current date, or a random number. Some of these are true environment variable and will be expanded by all functions that handle environment variables. Others, like %CD% simply look like environment variables and will only be expanded by some functions and shells. They are not case sensitive.

This variable points to the current directory. Equivalent to the output of the command cd</tt> when called without arguments.
 * %CD%

This variable expands to the current date. The date is displayed according to the current user's date format preferences.
 * %DATE%

The following is a way of reformatting the date and time for use in file copies. The example assumes UK format of day month year and the time is set for a 24 hour clock. This variable points to the current error level. If there was an error in the previous command, this is what you need to check against to find out about that.
 * %ERRORLEVEL%

This variable returns a random number between 0 and 32767
 * %RANDOM%

This variable points to the current time. The time is displayed according to the current user's time format preferences.
 * %TIME%

System path variables
These variables refer to locations of critical operating system resources, and as such generally are not user-dependent.

Contains the full path to the Application Data folder of the logged-in user. Does not work on Windows NT 4.0 SP6 UK.
 * %AppData%

This variable contains the full path to the command processor; on Windows NT based operating systems this is cmd.exe</tt>, while on Windows 9x and ME it is the DOS command processor, COMMAND.COM</tt>.
 * %ComSpec%

This variable contains a semicolon-delimited (do not put spaces in between) list of directories in which the command interpreter will search for executable files. Equivalent to the UNIX $PATH variable.
 * %PATH%

This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on English-language systems is C:\Program Files</tt>. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86)</tt> and %ProgramW6432% which defaults to C:\Program Files</tt>. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by Windows-on-Windows 64-bit redirection).
 * %ProgramFiles%

This variable points to Common Files directory. The default is C:\Program Files\Common Files</tt>.
 * %CommonProgramFiles%

The %SystemDrive% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the drive upon which the system folder was placed. Also see next item.
 * %SystemDrive%

The value of %SystemDrive% is in most cases C:</tt>.

The %SystemRoot% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the location of the system folder, including the drive and path.
 * %SystemRoot%

The drive is the same as %SystemDrive% and the default path on a clean installation depends upon the version of the operating system. By default, on a clean installation:
 * Windows NT 5.1 (Windows XP) and newer versions use \WINDOWS</tt>
 * Windows NT 5.0 (Windows 2000), Windows NT 4.0 and Windows NT 3.1 use \WINNT</tt>
 * Windows NT 3.5x uses \WINNT35</tt>

This variable points to the Windows directory (on Windows NT-based operating systems it is identical to the %SystemRoot% variable, above). If the System is on drive C: then the default values are:
 * %WinDir%
 * C:\WINDOWS</tt> on Windows 95, Windows 98, Windows Me, Windows XP, Windows Server 2003, Windows Vista and Windows Server 2008
 * C:\WINNT</tt> for Windows NT 4, and Windows 2000

Note that Windows NT 4 Terminal Server Edition by default installs to C:\WTSRV</tt>.

User management variables
These 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.

The %AllUsersProfile% variable expands to the full path to the All Users profile directory. This profile contains resources and settings that are used by all system accounts. Shortcut links copied to the All Users' Start menu or Desktop folders will appear in every user's Start menu or Desktop, respectively.
 * %AllUsersProfile%

The variable holds the name of the Workgroup or Windows Domain to which the current user belongs. The related variable, %LOGONSERVER%, holds the hostname of the server that authenticated the current user's logon credentials (name and password). For Home PCs, and PCs in a Workgroup, the authenticating server is usually the PC itself. For PCs in a Windows Domain, the authenticating server is a domain controller (a primary domain controller, or PDC, in Windows NT 4-based domains).
 * %UserDomain%

The %UserProfile% variable is a special system-wide environment variable found on Microsoft Windows NT and its derivatives. Its value is the location of the current user's profile directory, in which is found that user's HKCU registry hive (<tt>NTUSER</tt>).
 * %UserProfile%

Users can also use the %USERNAME% variable to determine the active users login identification.

Unix

 * environ(7): user environment – Linux Conventions and Miscellany Manual

Windows

 * Environment Variable Reference &mdash; Has a list showing which environment variables are for 9x WinNTx etc
 * Accessing Environment Variables &mdash; Has a list showing which environment variables are for 9x WinNTx etc
 * Windows XP Command Shell Overview with a list of environment variables &mdash; Microsoft.com
 * How To Manage Environment Variables in Windows XP &mdash; Microsoft.com
 * Path Manager (pathman.exe) &mdash; Command line tool from Microsoft for editing PATH environment variable on Windows
 * Environment Variables in Windows XP &mdash; Computer Education
 * RapidEE (Rapid Environment Editor) &mdash; Windows environment variables editor
 * (EnvMan) Windows Environment Variables Manager &mdash; Environment Variables Editor for Windows
 * Managing Search PATH on Windows &mdash; Easy interactive way of editing PATH environment variable on Windows
 * Dealing with Environment Variables in c# &mdash; How to set environment variables in c#


 * Getting Environment Variables in .NET &mdash; How to get environment variables in .NET