What are Environment Variables?
Environment variables hold values related to the current environment, like the Operating System or user sessions.
Path
One of the most well-known is called PATH on Windows, Linux and Mac OS X. It specifies the directories in which executable programs* are located on the machine that can be started without knowing and typing the whole path to the file on the command line. (Or in Windows, the Run dialog in the Start Menu or Windows Key+R).
On Linux and Mac OS X, it usually holds all bin and sbin directories relevant for the current user. On Windows, it contains at least the C:\Windows and C:\Windows\system32 directories — that's why you can run calc.exe or notepad.exe from the command line or Run dialog, but not firefox.exe. (Firefox is located in C:\Program Files\Mozilla Firefox. For information on how to include Firefox, go here.)
For example, typing calc (the .exe can be omitted) in the command line on Windows will start up the Windows Calculator.
* You can add support for file extensions other than .exe by editing %PATHEXT%.
Other
Other variables might tell programs what kind of terminal is used (TERM on Linux/Mac OS X), or, on Windows, where the Windows folder is located (e.g. %WINDIR% is C:\Windows).
Creating new environment variables
In Windows, Linux and Unix, it's possible to create new environment variables, whose values are then made available to all programs upon launch.
You can use this when writing scripts or programs that are installed or deployed to multiple machines and need to reference values that are specific to these machines. While a similar effect can be achieved using program-specific configuration settings, it's easier to do this using an environment variable if multiple programs need to access the same value.
Windows
GUI
In Windows, open Control Panel » System » Advanced » Environment Variables. They are separated into user and machine specific values. You can view and edit their values there. Their current values upon launch are made available to all programs.
There is also Rapid Environment Editor, which helps setting and changing environment variables in Windows without the need to go deep into the system settings.
Command Line
Format
Environment Variables in Windows are denoted with moduli surrounding the name:
%name%
echo
To display an environment variable's value in cmd.exe, type echo %name%.
C:\>echo %USERPROFILE%
C:\Users\Daniel
set
To create/set a variable, use set varname=value:
C:\>set FunnyCatPictures=C:\Users\Daniel\Pictures\Funny Cat Pictures
C:\>set FunnyCatPicturesTwo=%USERPROFILE%\Pictures\Funny Cat Pictures 2
To append/add a variable, use set varname=value;%varname%:
C:\>set Penguins=C:\Windows
C:\>set Penguins=C:\Linux;%Penguins%
C:\>echo %Penguins%
C:\Windows;C:\Linux
setx
To create/set a variable permanently, use setx varname "value":
C:\>setx FunnyCatPictures "C:\Users\Daniel\Pictures\Funny Cat Pictures"
[Restart CMD]
C:\>echo %FunnyCatPictures%
C:\Users\Daniel\Pictures\Funny Cat Pictures
Unlike set, there is no equals sign and the path should be enclosed in quotes if it contains any spaces. Note that if %PATH% is included, there will be spaces, so it is best to include quotes around paths that contain any variables.
You must manually add setx to versions of Windows earlier than Vista.
Windows XP Service Pack 2 Support Tools
List of Windows Environment Variables
Here is a list of default environment variables 1, which are built into Windows. Some examples are: %WINDIR%, %WINDIR%, and %APPDATA%.
Linux
Format
Environment Variables in Linux are denoted with a $ dollar sign in front:
$var
Locations
Linux (and Mac OS X) have several locations where these values can be declared, with varying scopes.
For example:
/etc/profile is made available system-wide, while
- a user's
~/.profile (in his home directory) will only apply to the user.
Setting values in ~/.bashrc or ~/.bash_profile will only apply to a user's bash sessions. Which of these is used depends on how bash is invoked (whether it's a login shell). In most cases, setting the variables in .profile is enough.
How to set a variable
These files are regular shell scripts and can contain more than just environment variable declarations. To set an environment variable, use export, like either:
var=value
export var
Or:
export var=value
To show your current environment in a terminal, enter env. Output will be displayed as key=value pairs, one on each line.
Mac OS X
As Mac OS X is based on Unix, the above files work too. Additionally, the PATH can be added to using the following files:
/etc/paths contains all default directories that are added to the path, like /bin and /usr/sbin.
- All files in
/etc/paths.d/ — this way, program installers can add their bin directories to the path without editing in users' or the system's configuration files.
These files simply contain one path, e.g. /usr/bin, per line.
External Links:
Environment Variables in XP
Windows XP Service Pack 2 Support Tools (Includes setx)
Environment Variables in Windows Vista and Windows 7
Adding executables to the Run Dialog Box
Mac OSX Tips - Setting Environment Variables