Environment variable 349504 226010941 2008-07-16T12:51:08Z 212.130.183.202 /* Default Values on Microsoft Windows */ added the vista equivalent of xp's allusersprofile '''Environment variables''' are a set of dynamic [[Value (computer science)|value]]s that can affect the way running [[computer process|processes]] will behave on a computer. ==Synopsis== In all [[Unix]] and [[Unix-like]] systems, each process has its own private set of environment [[variable]]s. 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 (more exactly, between <tt>fork</tt> and <tt>exec</tt>). 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 (computing)|PATH]] which lists directories the [[Shell (computing)|shell]] searches, for the commands the [[User (computing)|user]] may type. *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 terminal|dumb]]'''). *CVS_RSH ([[Unix-like]]) is used to tell [[Concurrent Versions System|CVS]] which [[Remote Shell|rsh]]-like program to use. *MAIL ([[Unix-like]]) is used to indicate where a user's mail is to be found. [[Shell script]]s 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 on a DOS or Windows system, the user could type in this command: echo %PATH% Other scripting and shell environments sometimes use other symbols for the same purpose, such as the commonly used dollar sign syntax used by many UNIX shells: echo $PATH ===DOS and Windows=== In DOS and Windows, the '''<tt>SET</tt>''' command without any arguments displays all environment variables along with their values. To set a variable to a particular value, use: SET ''VARIABLE''=''value'' 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 applications SETX.EXE. The most common method of setting an environment variable in Windows is via <Control Panel:System:Advanced:Environment Variables>. To see the current value of a particular variable, use: SET ''VARIABLE'' or ECHO ''%VARIABLE%'' ===UNIX=== The '''<tt>[[env (shell)|env]]</tt>''', '''<tt>set<tt>''', and '''<tt>printenv<tt>''' commands display all environment variables and their values. <tt>[[env (shell)|env]]</tt> and <tt>set<tt> are also used to set environment variables and are often incorporated directly into the shell. <tt>printenv<tt> 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_shell|Bourne]], [[bash]], and related shells setenv ''VARIABLE'' ''value'' # for [[C shell|csh]] and related shells === Unexported variables === In UNIX shells, variables may be assigned without the '''<tt>export</tt>''' keyword. Variables defined in this way are displayed by the '''<tt>set</tt>''' 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. ==Common environment variables== ===Examples of DOS environment variables=== ;[[ComSpec|%COMSPEC%]] This variable contains the full path to the command processor, command.com. ;%PATH% 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. ;%TEMP% and %TMP% These variables contain the path to the directory where temporary files should be stored. Note that in MS-DOS 5 %TEMP% sometimes pointed to C:\DOS so removing all files in %TEMP% would cause great problems to say the least, something that the main creator of the web browser [[Arachne (web browser)|Arachne]] wasn't aware of and thus giving Arachne much unwarranted media criticism. ===Examples of UNIX environment variables=== ;$PATH 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)]] ;$HOME 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 <tt>getpwuid</tt> and <tt>getuid</tt>, <tt>$HOME</tt> is often used for convenience in various shell scripts (and other contexts). ;$DISPLAY Contains the identifier for the display that [[X11]] programs should use by default. ;$LD_LIBRARY_PATH On many Unix systems with a [[dynamic linker]], contains a colon-separated list of directories that the dynamic linker should search for [[shared object]]s when building a process image after <tt>exec</tt>, before searching in any other directories. ;$LANG, $LC_ALL LANG expands to the default system [[locale]]; LC_ALL can be used to override this. For example, if its value is <code>pt_BR</code>, then the language is set to (Brazilian) Portuguese and the locale to Brazil. ;$TZ Refers to [[Time zone]]. It can takes values like GMT, AST, etc. ===Examples from Microsoft Windows=== ==== Discrete value variables ==== These variables generally expand to discrete values, such as the [[working directory|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. ;%CD% This variable points to the current directory. Equivalent to the output of the command <tt>cd</tt> when called without arguments. ;%DATE% This variable expands to the current date. The date is displayed according to the current user's [[Calendar date|date format]] preferences. 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. @Echo Off Echo %DATE% %TIME% SET DAY=%DATE:~4,2% SET MTH=%DATE:~7,2% SET YR=%DATE:~10,4% SET HR=%TIME:~0,2% SET HR0=%TIME:~0,1% IF "%HR0%"==" " SET HR=0%TIME:~1,1% SET MIN=%TIME:~3,2% SET SEC=%TIME:~6,2% SET MYDATE=%YR%%MTH%%DAY%-%HR%%MIN%%SEC% Echo %MYDATE% ;%ERRORLEVEL% 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. ;%RANDOM% This variable returns a random number between 0 and 32767 ;%TIME% This variable points to the current time. The time is displayed according to the current user's time format preferences. ==== System path variables ==== These variables refer to locations of critical operating system resources, and as such generally are not user-dependent. ;%AppData% Contains the full path to the Application Data folder of the logged-in user. Does not work on Windows NT 4.0 SP6 UK. ;%[[ComSpec]]% This variable contains the full path to the Windows NT command processor, <tt>cmd.exe</tt>. ;%PATH% 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. ;%ProgramFiles% This variable points to Program Files directory, which stores all the installed program of Windows and others. The default on english-language systems is <tt>C:\Program Files</tt>. In 64-bit editions of Windows (XP, 2003, Vista), there are also '''%ProgramFiles(x86)%''' which defaults to <tt>C:\Program Files (x86)</tt> and '''%ProgramW6432%''' which defaults to <tt>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 [[WoW64|Windows-on-Windows 64-bit]] redirection). ;%CommonProgramFiles% This variable points to Common Files directory. The default is <tt>C:\Program Files\Common Files</tt>. ;%SystemDrive% 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. The value of %SystemDrive% is in most cases <tt>C:</tt>. ;%SystemRoot% 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. 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 by default use <tt>\WINDOWS</tt> * Windows NT 5.0 ([[Windows 2000]]), Windows NT 4.0 and Windows NT 3.1 by default use <tt>\WINNT</tt> * Windows NT 3.5x by default uses <tt>\WINNT35</tt> ;%WinDir% This variable points to the Windows directory. If the System is on drive C: then the default values are: *<tt>C:\WINDOWS</tt> on [[Windows 95]], [[Windows 98]], [[Windows Me]], [[Windows XP]] and [[Windows Vista]] *<tt>C:\WINNT</tt> for [[Windows NT 4]], Windows 2000, and [[Windows Server 2003]] ==== User management variables ==== These variables store information related to resources and settings owned by various [[user profile]]s 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. ;%AllUsersProfile% 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. [[Computer shortcut|Shortcut]] links copied to the All Users' Start menu or Desktop folders will appear in every user's Start menu or [[Desktop metaphor|Desktop]], respectively. ;%UserDomain% The variable holds the name of the [[Workgroup]] or [[Windows Server domain|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). ;%UserProfile% 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>). Users can also use the '''%USERNAME%''' variable to determine the active users login identification. ==Default Values on Microsoft Windows== {| class="wikitable" ! Variable ! Windows XP ! Windows Vista |- | %ALLUSERSPROFILE% | C:\Documents and Settings\All Users | C:\ProgramData |- | %APPDATA% | C:\Documents and Settings\{username}\Application Data | C:\Users\{username}\AppData\Roaming |- | %COMPUTERNAME% | {computername} | {computername} |- | %COMSPEC% | C:\Windows\System32\cmd.exe | C:\Windows\System32\cmd.exe |- | %HOMEDRIVE% | C:\ | C:\ |- | %HOMEPATH% | C:\Documents and Settings\{username} | C:\Users\{username} |- | %PATH% | C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths} | C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;{plus program paths} |- | %PATHEXT% | .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.WSF;.WSH | .com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc |- | %PROGRAMFILES% | C:\Program Files | C:\Program Files |- | %PROGRAMFILES(X86)% | C:\Program Files (x86) (only in 64-bit version) | C:\Program Files (x86) (only in 64-bit version) |- | %PROMPT% | Code for current command prompt format. Code is usually $P$G | Code for current command prompt format. Code is usually $P$G |- | %SYSTEMDRIVE% | C:\ | C:\ |- | %SystemRoot% | The Windows directory, usually C:\Windows, formerly C:\WINNT | C:\Windows |- | %TEMP% and %TMP% | C:\Documents and Settings\{username}\Local Settings\Temp | C:\Users\{username}\AppData\Local\Temp |- | %USERNAME% | {username} | {username} |- | %USERPROFILE% | C:\Documents and Settings\{username} | C:\Users\{username} |- | %WINDIR% | C:\Windows | C:\Windows |- | %PUBLIC% | | C:\Users\Public |} ==See also== *[[List of Unix programs]] *[[List of DOS commands]] ==External links== *{{man|7|environ||user environment}} *[http://www.scriptlogic.com/support/CustomScripts/environmentVariableReference.html Environment Variable Reference] &mdash; ''Has a list showing which environment variables are for 9x WinNTx etc'' *[http://www.freeweb.hu/wsh2/ch07c.html Accessing Environment Variables] &mdash; ''Has a list showing which environment variables are for 9x WinNTx etc'' *[http://technet.microsoft.com/en-gb/library/bb490954.aspx Windows XP Command Shell Overview] with a list of environment variables &mdash; ''Microsoft.com'' *[http://support.microsoft.com/default.aspx?scid=kb;en-us;310519 How To Manage Environment Variables in Windows XP] &mdash; ''Microsoft.com'' *[http://vlaurie.com/computers2/Articles/environment.htm Environment Variables in Windows XP] &mdash; ''Computer Education'' *[http://www.rapidee.com RapidEE (Rapid Environment Editor)] &mdash; ''Windows environment variables editor'' *[http://env-man.blogspot.com/2007/04/envman-user-guide.html (EnvMan) Windows Environment Variables Manager] &mdash; ''Environment Variables Editor for Windows'' *[http://redmondlab.net/path Managing Search PATH on Windows] &mdash; ''Easy interactive way of editing PATH environment variable on Windows'' [[Category:Operating system technology]] [[Category:Configuration]] [[Category:Variable (computer programming)]] [[bg:Променливи на средата]] [[de:Umgebungsvariable]] [[es:Variable de entorno]] [[fr:Variable d'environnement]] [[ko:환경 변수]] [[it:Variabile d'ambiente]] [[ja:環境変数]] [[pl:Zmienna środowiskowa]] [[pt:Variável de ambiente]] [[ru:Переменные среды]] [[fi:Ympäristömuuttuja]]