Environment variable: Difference between revisions

The educational technology and digital learning wiki
Jump to navigation Jump to search
 
mNo edit summary
 
(11 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{incomplete}}
== Definition ==
== Definition ==


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


Even advanced Windows users should know about these. It can make your life much easier when you install certain software, e.g. Java applications, the [[Flex]] compiler, etc.
Even advanced Windows users should know about these. It can make your life much easier when you install certain software, e.g. Java applications, the [[Adobe Flex]] SDK, etc.
You can set variables:
You can set variables:
* In the system configuration
* In the system configuration
Line 11: Line 12:


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 (more exactly, between fork and exec). 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
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 (more exactly, between fork and exec). 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
Copyright Notice: This information is available under the [http://en.wikipedia.org/wiki/Wikipedia:Text_of_the_GNU_Free_Documentation_License GNU Free Documentation License] (since most contents were taken from Wikipedia).


== Unix/Linux ==
== Unix/Linux ==
Line 21: Line 18:


;$PATH
;$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)]]
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 [http://en.wikipedia.org/wiki/Path (computing)]


;$HOME
;$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).
Contains the location of the user's [http://en.wikipedia.org/wiki/home_directory 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
;$DISPLAY
Contains the identifier for the display that [[X11]] programs should use by default.
Contains the identifier for the display that [http://en.wikipedia.org/wiki/X11 X11] programs should use by default.


;$LD_LIBRARY_PATH
;$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>.
On many Unix systems with a [http://en.wikipedia.org/wiki/dynamic_linker dynamic_linker], contains a colon-separated list of directories that the dynamic linker should search for [http://en.wikipedia.org/wiki/shared object]s when building a process image after <tt>exec</tt>.


;$LANG, $LC_ALL
;$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.
LANG expands to the default system [http://en.wikipedia.org/wiki/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
;$TZ
Refers to [[Time zone]].  It can takes values like GMT, AST, etc.
Refers to [http://en.wikipedia.org/wiki/Time zone].  It can takes values like GMT, AST, etc.
 
=== Getting variables ===


=== Getting and setting variables ===
In all [http://en.wikipedia.org/wiki/Unix Unix] and [http://en.wikipedia.org/wiki/Unix-like Unix-like] systems, each process has its own private set of environment [http://en.wikipedia.org/wiki/variable 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 [http://en.wikipedia.org/wiki/Unix Unix] [http://en.wikipedia.org/wiki/operating_system operating system] flavors as well as [http://en.wikipedia.org/wiki/DOS DOS] and [http://en.wikipedia.org/wiki/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.


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  
Examples of environment variables include  
#The [[Path (computing)|PATH]] which lists directories the [[Shell (computing)|shell]] searches, for the commands the [[User (computing)|user]] may type.   
#The [http://en.wikipedia.org/wiki/Path_(computing) PATH] which lists directories the [http://en.wikipedia.org/wiki/Shell_(computing) shell] searches, for the commands the [http://en.wikipedia.org/wiki/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]].
#HOME ([http://en.wikipedia.org/wiki/Unix-like Unix-like]) and userprofile (Microsoft Windows) indicate where a user's [http://en.wikipedia.org/wiki/home directory] is located in the [http://en.wikipedia.org/wiki/file_system file system].
#TERM ([[Unix-like]]) specifies the type of [[computer terminal]] or [[terminal emulator]] being used (e.g., '''[[vt100]]''' or '''[[dumb terminal|dumb]]''').
#TERM ([http://en.wikipedia.org/wiki/Unix-like Unix-like]) specifies the type of [http://en.wikipedia.org/wiki/computer_terminal computer terminal] or [http://en.wikipedia.org/wiki/terminal_emulator emulator] being used (e.g., '''[http://en.wikipedia.org/wiki/vt100 vt100]''' or '''[http://en.wikipedia.org/wiki/dumb_terminal dumb]''').
#CVS_RSH ([[Unix-like]]) is used to tell [[Concurrent Versions System|CVS]] which [[Remote Shell|rsh]]-like program to use.
#CVS_RSH ([http://en.wikipedia.org/wiki/Unix-like unix-like]) is used to tell [http://en.wikipedia.org/wiki/Concurrent_Versions_System CVS] which [http://en.wikipedia.org/wiki/Remote_Shell rsh]-like program to use.
#MAIL ([[Unix-like]]) is used to indicate where a user's mail is to be found.
#MAIL ([http://en.wikipedia.org/wiki/Unix-like 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.
[[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.
Line 51: Line 49:
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, 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.
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 [http://en.wikipedia.org/wiki/windows_registry windows registry] or set in autoexec.bat.
 


Unix scripting and shell environments commonly use the dollar sign syntax, e.g.
Unix scripting and shell environments commonly use the dollar sign syntax, e.g.
Line 58: Line 55:
will print the value of $PATH
will print the value of $PATH


The '''<tt>[[env]]</tt>''', '''<tt>set<tt>''', and '''<tt>printenv<tt>''' commands display all environment variables and their values.  <tt>[[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.
=== Setting variables under Unix ===
 
The '''<tt>[http://en.wikipedia.org/wiki/env env]</tt>''', '''<tt>set</tt>''', and '''<tt>printenv</tt>''' commands display all environment variables and their values.  <tt>[http://en.wikipedia.org/wiki/env 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.
In UNIX, the following commands can also be used, but are often dependent on a certain shell.
  export ''VARIABLE''=''value''  # for [http://en.wikipedia.org/Bourne_shell|Bourne], [http://en.wikipedia.org/bash], and related shells
  export ''VARIABLE''=''value''  # for [http://en.wikipedia.org/wiki/Bourne_shell Bourne], [http://en.wikipedia.org/wik/bash bash], and related shells
  setenv ''VARIABLE'' ''value''  # for [http://en.wikipedia.org/C shell|csh] and related shells
  setenv ''VARIABLE'' ''value''  # for [http://en.wikipedia.org/wiki/C_shell csh] and related shells
 
 
 
 
=== Unexported environment variables ===


In UNIX, 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''' inherited by the child processes.
In UNIX, 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''' inherited by the child processes.
Line 77: Line 71:
=== Security ===
=== 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.
On [http://en.wikipedia.org/wiki/Unix Unix], a [http://en.wikipedia.org/wiki/setuid 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.


== DOS / Windows ==
== DOS / Windows ==


=== Examples ===
=== Examples ===


;[[ComSpec|%COMSPEC%]]
;[http://en.wikipedia.org/wiki/ComSpec %COMSPEC%]
This variable contains the full path to the command processor, command.com.
This variable contains the full path to the command processor, command.com.


Line 93: Line 86:
;%TEMP% and %TMP%
;%TEMP% and %TMP%
These variables contain the path to the directory where temporary files should be stored.
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)]] wasn't aware of and thus giving Arachne much unwarranted media criticism.


==== Discrete value variables ====
==== 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.
 
These variables generally expand to discrete values, such as the [http://en.wikipedia.org/wiki/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.


;%CD%
;%CD%
Line 102: Line 95:


;%DATE%
;%DATE%
This variable expands to the current date.  The date is displayed according to the current user's [[Calendar date|date format]] preferences.
This variable expands to the current date.  The date is displayed according to the current user's [http://en.wikipedia.org/wiki/Calendar_date date format] preferences.


;%ERRORLEVEL%
;%ERRORLEVEL%
Line 119: Line 112:
Contains the full path to the Application Data folder of the logged-in user.  Does not work on Windows NT 4.0 SP6 UK.
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]]%
;%[http://en.wikipedia.org/wiki/ComSpec ComSpec]%
This variable contains the full path to the Windows NT command processor, <tt>cmd.exe</tt>.
This variable contains the full path to the Windows NT command processor, <tt>cmd.exe</tt>.


Line 126: Line 119:


;%ProgramFiles%
;%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).
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 [http://en.wikipedia.org/wiki/WoW64|Windows-on-Windows 64-bit] redirection).


;%CommonProgramFiles%
;%CommonProgramFiles%
Line 132: Line 125:


;%SystemDrive%
;%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.
The %SystemDrive% variable is a special system-wide environment variable found on [http://en.wikipedia.org/wiki/Microsoft Microsoft] [http://en.wikipedia.org/wiki/Windows_NT NT] and its derivatives.  Its value is the drive upon which the system folder was placed.


The value of %SystemDrive% is in most cases <tt>C:</tt>.  The system drive cannot be altered once the operating system is running.
The value of %SystemDrive% is in most cases <tt>C:</tt>.  The system drive cannot be altered once the operating system is running.
Line 140: Line 133:


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,
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.1 ([http://en.wikipedia.org/wiki/Windows_XP 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 5.0 ([http://en.wikipedia.org/wiki/Windows_2000 Win 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>
* Windows NT 3.5x by default uses <tt>\WINNT35</tt>


;%WinDir%
;%WinDir%
This variable points to the Windows directory. If the System is on drive C: then the default values are:
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]], [[Windows Server 2003]] and [[Windows Vista]]
*<tt>C:\WINDOWS</tt> on [http://en.wikipedia.org/wiki/Windows_95 Win95], [http://en.wikipedia.org/wiki/Windows 98], [http://en.wikipedia.org/wiki/Windows_Me Me], [http://en.wikipedia.org/wiki/Windows XP], [http://en.wikipedia.org/wiki/Windows_Server_2003 Win Server 2003] and [http://en.wikipedia.org/wiki/Windows_Vista Vista]
*<tt>C:\WINNT</tt> for [[Windows NT 4]] and Windows 2000
*<tt>C:\WINNT</tt> for NT4 and Windows 2000


==== User management variables ====
==== 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.
These variables store information related to resources and settings owned by various [http://en.wikipedia.org/wiki/user_profile 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%
;%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.
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.  [http://en.wikipedia.org/wiki/Computer_shortcut Shortcut] links copied to the All Users' Start menu or Desktop folders will appear in every user's Start menu or [http://en.wikipedia.org/wiki/Desktop_metaphor Desktop], respectively.


;%UserDomain%
;%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).
The variable holds the name of the [http://en.wikipedia.org/wiki/Workgroup workgroup] or [http://en.wikipedia.org/wiki/Windows Server domain|Windows Domain] to which the current user belongs.  The related variable, %LOGONSERVER%, holds the [http://en.wikipedia.org/wiki/hostname 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 [http://en.wikipedia.org/wiki/domain_controller domain controller] (a primary domain controller, or PDC, in Windows NT 4-based domains).


;%UserProfile%
;%UserProfile%
Line 219: Line 212:
|}
|}


=== Getting a value ===
=== Getting a value in windows ===


In DOS and Windows, the '''<tt>SET</tt>''' command without any arguments displays all environment variables along with their values.   
In DOS and Windows, the '''<tt>SET</tt>''' command without any arguments displays all environment variables along with their values.   


=== Setting a value in a command file or in the shell ===
=== Setting a variable in a Windows command file ===
 
; In a command file e.g. my.bat or my.cmd or in the shell:


To set a variable to a particular value in DOS-WINDOWS, use:
To set a variable to a particular value in DOS-WINDOWS, use:
  SET ''VARIABLE''=''value'
  SET ''VARIABLE''=''value'


== Links ==
=== Setting a variable in Win XP ===
 
; To modify a variable in the system (example 1 for XP)
 
On a french speaking Windows XP system (sorry) the procedure to add something to a path variable is the following
* ''Démarrer->Paramètres->Panneau de configuration->Système''
* Select the panels: ''Avancé->Variables d'environnment''
 
On an English system:
* ''Start->Parameters->Configuration Panel->System''
* Or alternatively, right-click on ''My Computer Icon->Properties''
* Select the panels: ''Advanced->Environment variables''
 
Then:
* Change the "Path" variable in the '''user variables pane''' by adding the flex path at the end of the existing path variable. The result may look like this:
  %Path%;c:\Flex SDK 2\bin
* If the "path" variable doesn't exist, create it and copy the above line.
 
Do not remove the other pathes ! Do not edit the system variables if you don't know what you do or you may regret it bitterly, because your system will stop working if do it wrongly. !
 
Here is a screendump showing some of the (french) Windows panels:
[[image:win-xp-change-path.png|thumb|800px|none|Changing the User Path variable under Windows XP (french)]]
 
; To add a new variable in the system (example 2 for XP)
E.g. the JAVA_HOME
# Right click on the 'My Computer' icon, 'Properties->Advanced''.
# Click on 'Environment Variables'.
# Under ''user variables'' or ''system variables'', click 'New'.
# Add a new variable, e.g. JAVA_HOME and enter the value, e.g. directory containing Java (e.g. C:\Program Files\Java\jdk1.5.0_04).
 
=== Setting a variable in Windows 7 ===
 
The principle is exactly the same as in XP or VISTA.
 
With respect to XP, you'll have to click some more:
 
On a french speaking system:
* Menu->Panneau de configuration
* ''Système et sécurité'' ->''Système'' -> Paramètres système avancés
* Variable d'environnement
 
[[image:win-7-change-path.jpg|thumb|800px|none|Changing the User Path variable under Windows 7 (french, from top left, clockwise)]]
 
In an English system, it's probably:
* Start menu -> Configuration panel
* System and securtiy -> System -> Advanced System parameters
* Environment variables
 
'''Tip''': Instead of wading through this complex windows/tabs/menu hierarchy you also can type "variable" in the search box of the start menu. The click the one of the links on top :)


== Links ==
== Links ==


* [http://en.wikipedia.org/wiki/Environment_variable Environment Variable] (Wikipedia)
* [http://en.wikipedia.org/wiki/Environment_variable Environment Variable] (Wikipedia)
* {{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.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://www.freeweb.hu/wsh2/ch07c.html Accessing Environment Variables] &mdash; ''Has a list showing which environment variables are for 9x WinNTx etc''
Line 243: Line 284:
*[https://sourceforge.net/projects/env-man/ (EnvMan) Windows Environment Variables Manager] &mdash; ''Environment Variables Editor for Windows''
*[https://sourceforge.net/projects/env-man/ (EnvMan) Windows Environment Variables Manager] &mdash; ''Environment Variables Editor for Windows''


== Acknowledgements and Copyright modification ==


'''Copyright Notice''': Information in '''this''' article is also available under the [http://en.wikipedia.org/wiki/Wikipedia:Text_of_the_GNU_Free_Documentation_License GNU Free Documentation License] (since most contents were taken from Wikipedia).


[[Category: Technologies]]
[[Category: Installation tips]]
[[Category: Installation tips]]
[[Category:Server administration]]

Latest revision as of 19:15, 13 September 2010

Definition

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

Even advanced Windows users should know about these. It can make your life much easier when you install certain software, e.g. Java applications, the Adobe Flex SDK, etc. You can set variables:

  • In the system configuration
  • In configuration files of some applications
  • In shell scripts.
  • In the command line (shell)

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 (more exactly, between fork and exec). 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

Unix/Linux

Examples

$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 (computing)

$HOME

Contains the location of the user's directory. Although the current user's home directory can also be found out through the C functions getpwuid and getuid, $HOME 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 objects when building a process image after exec.

$LANG, $LC_ALL

LANG expands to the default system [1]; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.

$TZ

Refers to zone. It can takes values like GMT, AST, etc.

Getting variables

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 (more exactly, between fork and exec). All Unix operating system flavors as well as DOS and 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

  1. The PATH which lists directories the shell searches, for the commands the user may type.
  2. HOME (Unix-like) and userprofile (Microsoft Windows) indicate where a user's directory is located in the file system.
  3. TERM (Unix-like) specifies the type of computer terminal or emulator being used (e.g., vt100 or dumb).
  4. CVS_RSH (unix-like) is used to tell CVS which rsh-like program to use.
  5. MAIL (unix-like) is 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.

Unix scripting and shell environments commonly use the dollar sign syntax, e.g.

echo $PATH

will print the value of $PATH

Setting variables under Unix

The env, set, and printenv commands display all environment variables and their values. env and set are also used to set environment variables and are often incorporated directly into the shell. printenv 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

In UNIX, variables may be assigned without the export keyword. Variables defined in this way are displayed by the set command, but are not inherited by the child processes.

VARIABLE=value

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.

DOS / Windows

Examples

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

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.

%CD%

This variable points to the current directory. Equivalent to the output of the command cd when called without arguments.

%DATE%

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

%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, cmd.exe.

%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 C:\Program Files. In 64-bit editions of Windows (XP, 2003, Vista), there are also %ProgramFiles(x86)% which defaults to C:\Program Files (x86) and %ProgramW6432% which defaults to C:\Program Files. The %ProgramFiles% itself depends on whether the process requesting the environment variable is itself 32-bit or 64-bit (this is caused by 64-bit redirection).

%CommonProgramFiles%

This variable points to Common Files directory. The default is C:\Program Files\Common Files.

%SystemDrive%

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

The value of %SystemDrive% is in most cases C:. The system drive cannot be altered once the operating system is running.

%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 (XP) and newer versions by default use \WINDOWS
  • Windows NT 5.0 (Win 2000), Windows NT 4.0 and Windows NT 3.1 by default use \WINNT
  • Windows NT 3.5x by default uses \WINNT35
%WinDir%

This variable points to the Windows directory. If the System is on drive C: then the default values are:

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.

%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. Shortcut links copied to the All Users' Start menu or Desktop folders will appear in every user's Start menu or Desktop, respectively.

%UserDomain%

The variable holds the name of the workgroup or 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 (NTUSER).

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

Defaults:
Variable Data
%ALLUSERSPROFILE% C:\Documents and Settings\All Users
%APPDATA% C:\Documents and Settings\{username}\Application Data
%COMPUTERNAME% {computername}
%COMSPEC% C:\Windows\System32\cmd.exe
%HOMEDRIVE% C:
%HOMEPATH% \Documents and Settings\{username}
%PATH% C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem
%PATHEXT% .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF: .WSH
%PROGRAMFILES% C:\Program Files
%PROMPT% Code for current command prompt format. Code is usually $P$G
%SYSTEMDRIVE% The drive containing the Windows XP root directory, usually C:
%SYSTEMROOT% The Windows XP root directory, usually C:\Windows
%TEMP% and %TMP% C:\Documents and Settings\{username}\Local Settings\Temp
%USERNAME% {username}
%USERPROFILE% was C:\windows\Profile in Windows NT 4 and 2000

C:\Documents and Settings\{username} C:\Users\{username} under Windows Vista

%WINDIR% C:\Windows

Getting a value in windows

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

Setting a variable in a Windows command file

In a command file e.g. my.bat or my.cmd or in the shell

To set a variable to a particular value in DOS-WINDOWS, use:

SET VARIABLE=value'

Setting a variable in Win XP

To modify a variable in the system (example 1 for XP)

On a french speaking Windows XP system (sorry) the procedure to add something to a path variable is the following

  • Démarrer->Paramètres->Panneau de configuration->Système
  • Select the panels: Avancé->Variables d'environnment

On an English system:

  • Start->Parameters->Configuration Panel->System
  • Or alternatively, right-click on My Computer Icon->Properties
  • Select the panels: Advanced->Environment variables

Then:

  • Change the "Path" variable in the user variables pane by adding the flex path at the end of the existing path variable. The result may look like this:
 %Path%;c:\Flex SDK 2\bin
  • If the "path" variable doesn't exist, create it and copy the above line.

Do not remove the other pathes ! Do not edit the system variables if you don't know what you do or you may regret it bitterly, because your system will stop working if do it wrongly. !

Here is a screendump showing some of the (french) Windows panels:

Changing the User Path variable under Windows XP (french)
To add a new variable in the system (example 2 for XP)

E.g. the JAVA_HOME

  1. Right click on the 'My Computer' icon, 'Properties->Advanced.
  2. Click on 'Environment Variables'.
  3. Under user variables or system variables, click 'New'.
  4. Add a new variable, e.g. JAVA_HOME and enter the value, e.g. directory containing Java (e.g. C:\Program Files\Java\jdk1.5.0_04).

Setting a variable in Windows 7

The principle is exactly the same as in XP or VISTA.

With respect to XP, you'll have to click some more:

On a french speaking system:

  • Menu->Panneau de configuration
  • Système et sécurité ->Système -> Paramètres système avancés
  • Variable d'environnement
Changing the User Path variable under Windows 7 (french, from top left, clockwise)

In an English system, it's probably:

  • Start menu -> Configuration panel
  • System and securtiy -> System -> Advanced System parameters
  • Environment variables

Tip: Instead of wading through this complex windows/tabs/menu hierarchy you also can type "variable" in the search box of the start menu. The click the one of the links on top :)

Links

Acknowledgements and Copyright modification

Copyright Notice: Information in this article is also available under the GNU Free Documentation License (since most contents were taken from Wikipedia).