New Document
Environment variable

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

They are part of the operating environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

Details:

In all Unix and Unix-like systems, each process has its own separate 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 running fork and exec. Alternatively, from command shells such as bash, a user can change environment variables for a particular command invocation by indirectly invoking it via env or using the ENVIRONMENT_VARIABLE=VALUE notation. All Unix operating system flavors, MS-DOS, and Microsoft Windows have environment variables; however, they do not all use the same variable names. A running program can access the values of environment variables for configuration purposes.


    PATH - A list of directory paths. When the user types a command without providing the full path, this list is checked if it contains a path that leads to the command.
    HOME (Unix-like) and USERPROFILE (Microsoft Windows) - indicate where a user's home directory is located in the file system.
    HOME/{.AppName} (Unix-like) and APPDATA\{DeveloperName\AppName} (Microsoft Windows) - for storing application settings. Many open source programs incorrectly use USERPROFILE for application settings in Windows - USERPROFILE should only be used in dialogs that allow user to choose between paths like Documents/Pictures/Downloads/Music, for programmatic purposes APPDATA (roaming), LOCALAPPDATA or PROGRAMDATA (shared between users) is used.
    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.
    TEMP - location where processes can store temporary files

Assignment

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 user home directory, in most scripting environments, the user has to type:

echo $HOME

On DOS, OS/2 or Windows systems, the user has to type this:

echo %HOME%

In Windows PowerShell, the user has to type this:

Write-Output $HOME

The commands env, set, and printenv 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
Common environment variables
$PATHContains 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). It is equivalent to the Windows %PATH% variable.
$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 another directory.
$PWDThis variable points to the current directory. Equivalent to the output of the command pwd when called without arguments.
$DISPLAYContains the identifier for the display that X11 programs should use by default.
$LD_LIBRARY_PATHOn 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, before searching in any other directories.
$LANG, $LC_ALL, $LC_...LANG is used to set to the default locale. For example, if the locale values are pt_BR, then the language is set to (Brazilian) Portuguese and Brazilian practice is used where relevant. Different aspects of localization are controlled by individual LC_-variables (LC_CTYPE, LC_COLLATE, LC_DATE etc.). LC_ALL can be used to force the same locale for all aspects.
$TZRefers to Time zone. It can be in several formats, either specifying the timezone itself or referencing a file (in /usr/share/zoneinfo).
Previous                                                                                                                                                       Next

Back to Top