Environment variables of a Operating System hold values related to the current environment, like the user sessions. They are part of the 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
USERPROFILE variable to find the directory structure owned by the user running the process.
It specifies the directories in which executable programs are located on the machine. A executable present in Path directory can be started without knowing and typing the whole path to the file on the Terminal.
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
That’s why you can run
notepad.exe from the command line, but not firefox.exe (
C:\Program Files\Mozilla Firefox).
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%. The PathExt is an Environment Variable that stores a list of the file extensions for operation system to execute. When running a command line that does not contain an extension, the system uses the value of this environment variable to determine which extensions to look for and in what order, such as .com first, follow by .exe, .bat, .cmd, which happens to be the default value stored in the PathExt by Windows. To find out what’s in the PathExt, run the following command in the command prompt window.
Create New Environment Variable
Windows, Linux, Unix and MacOs allows to create new environment variables, whose values are then made available to all programs upon launch. To create environment variable on Windows, Open Control Panel > System > Advanced > Environment Variables.
Environment variables in Windows are separated into user and machine/system specific values. Environment Variables in Windows are denoted with percent signs (%) surrounding the name:
To display an environment variable’s value in cmd.exe, type echo %name%.
C:\>echo %USERPROFILE% C:\Users\Anand
To create/set a variable, use
set varname=value, for example
To append/add a variable, use
set varname=value;%varname%, for example
Breaking it down:
# Takes the current path and sets PATH to it set PATH=%PATH%; # Adds this directory to the path, because of 'set PATH' this is add to C:\Users\Anand\Pictures\EnvDemo
Environment variables set in this way are available for (the rest of) the duration of the Command Prompt process in which they are set, and are available to processes that are started after the variables were set. To create/set a variable permanently, use
setx varname "value".
Environment Variables in Unix Derivatives (FreeBSD, GNU / Linux, OS X) are prefixed with a dollar sign ($) such as
$HOME. Keep in mind that variable names are case-sensitive, i.e. $User and $USER are entirely unrelated from the shell’s point of view.
Unix derivatives define system wide variables in shell scripts located mostly in the /etc folder, but user-specific values may be given to those variables in scripts located in the home folder (e.g.,
.profile file in the home folder is a common place to define user variables.
To set an environment variable, use
export. To show your currently defined environment variables in a terminal, run
env. The outcome of below two lines are identical.
var=value; export var export var=value
Additionally to the files already mentioned, $PATH can be modified in these files:
- /etc/paths contains all default directories that are added to the path, like /bin and /usr/sbin.
- Any file in /etc/paths.d — commonly used by installers to make the executable files they provide available from the shell without touching system-wide or user-specific configuration files. These files simply contain one path per line. e.g., /Programs/Mozilla/Calendar/bin.