Monday, February 8, 2010

Environmental Variables

Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. these have been used for long in operating systems and are present in Unix, DOS and Windows environment.

Microsoft's definitions:

Environment variables are strings that contain information such as drive, path, or file name. They control the behavior of various programs. For example, the TEMP environment variable specifies the location in which programs place temporary files.

 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.
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 the script, although in Unix other variables are usually used for this.

Examples of DOS Env Variables:
%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.
%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.
Examples of UNIX ENV 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 file names to execute, and the shell attempts to execute the files directly).
$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). Using the environment variable also gives the user the possibility to point to an other directory.
$PWD : This variable points to the current directory. Equivalent to the output of the command pwd when called without arguments.
$TZ : Refers to Time zone. It can be in several formats, either specifying the timezone itself or referencing a file (in /usr/share/zoneinfo).
Example from Microsoft Windows: Case Insensitive
%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. For more info on how to set date variable refer %DATE%
 
%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.
 
Reference: 
Default values of Environment Variable on Microsoft windows
Further Related Study:
List of UNIX Utilities
 

No comments:

Post a Comment