Environment variable
From Wikipedia, the free encyclopedia
Environment variables are a set of dynamic values that can affect the way running processes will behave on a computer.
Contents |
[edit] 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 (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. Examples of environment variables include
- The PATH which lists directories the shell searches for commands the 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).
- CVS_RSH (Unix-like) is used to tell CVS which rsh-like program to use.
- 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 variables value (or removing it) inside a BATCH file will change the variable for the duration of command.coms existence.
In Unix, the environment variables are normally initialized during system startup by the system init scripts, and thence 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.
[edit] 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%
Although typing only PATH would be faster.
In the bash shell for Unix (also available for DOS and Windows), either of the two commands below could be used:
echo $PATH echo ${PATH}
Other scripting and shell environments sometimes use other symbols for the same purpose.
To set a variable to a particular value in DOS-WINDOWS, use:
SET VARIABLE=value
The Unix equivalent is:
export VARIABLE=value # for bash/sh and related shells setenv VARIABLE value # for csh and related shells
To remove a variable in DOS-WINDOWS, use:
SET VARIABLE=
[edit] Common environment variables
[edit] DOS
The SET command without any arguments displays all environment variables along with their values.
%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. 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 webbrowser Arachne (web browser) wasn't aware of and thus giving Arachne much unwarranted media critic.
[edit] Unix
Note that, on a Unix system, a lot of environment variables normally exist, many of them very significant for programs to be working normally. This list is only a very brief list of some of the more significant ones (some would probably argue that there are others which are more significant). Please see your Unix system manual for further information.
The env command displays all environment variables along with their values in Unix shells such as sh and csh.
- $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)
- $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 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 shared objects when building a process image after exec.
- $LANG, $LC_ALL
LANG expands to the default system locale; 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.
[edit] Microsoft Windows
The set command without any arguments displays all environment variables along with their values.
[edit] Discrete value variables
These variables generally expand to discrete values, such as the current working directory, the current date, or a random number.
- %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.
[edit] 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 is C:\Program Files.
- %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 Windows 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 (Windows XP) and newer versions by default use \WINDOWS
- Windows NT 5.0 (Windows 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:
- C:\WINDOWS on Windows 95, Windows 98, Windows Me, Windows XP, and Windows Server 2003
- C:\WINNT for Windows NT 4 and Windows 2000
[edit] 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 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.
[edit] 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.
[edit] External links
- Windows XP Command Shell Overview with a list of environment variables — Microsoft.com
- How To Manage Environment Variables in Windows XP — Microsoft.com
- Environment Variables in Windows XP — Computer Education
- WEVE (Windows Environment Variable Editor) A GUI editor for environment variables in Windows XP — WEVE GUI Editor
- scriptlogic.com