What are PATH and other environment variables, and how can I set or use them?

347

259

Questions about

  • setting environment variables
  • the PATH

are very common here, and in most cases the answers are very similar to each other. In the future it would be nice to have a good Q/A for this.

So the question is: What are environment variables, like the executable PATH, and how can I change and use them on major operating systems?

A good answer would include a simple explanation of what environment variables and especially PATH mean to the OS, as well as simple guidelines on how to set and read them accordingly.

slhck

Posted 2011-05-16T18:25:57.433

Reputation: 182 472

2

Tired of having to do it manually each time I needed to, I wrote some registry entries to add context menu entries for AddToPath and RemoveFromPath to folders, utilizing a free app called pathed.exe . Following is the link to the complete guide: http://www.addictivetips.com/windows-tips/add-remove-any-folder-to-path-variable-using-context-menu/ I can now very easily add folders to PATH variable or remove them from it, without having to edit any file or worry about messing up the formatting of the variable. Hope it helps. =)

– Haroon Q. Raja – 2012-04-16T14:48:42.013

In Windows 8, the fastest navigation to changing system and user environment variables is using search. <kbd>Win</kbd>+<kbd>W</kbd> brings up the search for all settings. Search for env and the needed options are listed. – FRIdSUN – 2014-02-25T00:10:41.280

I think it's faster and clearer by opening CMD and typing set, or PowerShell and typing Get-Childitem env:. – paradroid – 2014-02-25T00:17:23.730

1On Windows there is a shortcut for opening properties of System ie. Control Panel -> System It's WIN key on keyboard + Pause/Break (WIN+Break). This is extremely helpful and speeding up process of setting new environmental variables on Win8 because there you have to click through several windows in modern ui (Yeah. If it's modern then I'll grow a cactus on my palm) which is just annoying. Of course you can create powershell script or use setx command and don't worry any more about it :D – termil0r – 2012-11-08T16:59:11.363

If you're editing the path variable a lot (like when setting up a new system), it might help to have a shortcut to the System Properties dialog on the desktop. To do so, right click on the desktop, select New Shortcut, and enter systempropertiesadvanced.exe. Then you can click on the link to get to the System Properties dialog, then click on Environment Variables to get to the dialog with the path settings. – Brian Burns – 2015-03-09T18:12:48.353

Great post about it: http://www.windows-commandline.com/set-path-command-line/

– Royi – 2016-08-14T20:44:28.470

Answers

337

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 Win+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

  1. Open Control Panel » System » Advanced » Environment Variables.

  2. Type control sysdm.cpl,,3 in the Run dialog (Win+R) and click Environment Variables.
    For editing user variables you can also type

    %windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
    

    in the Run dialog.

  3. Right-click (My) Computer and click on Properties, or simply press Win+Break.

    • In XP click on Advanced » Environment Variables.
    • In Vista+ click on Advanced system settings » Environment Variables.
  4. There are many other ways of reaching the same place, such as by typing "environment variables" in the Start Menu/Screen search box and so on.

Environment variables in Windows are separated into user and machine/system 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. Another open source program for Windows with which the path environment can be edited very conveniently is Path Editor.

Command Line

Format

Environment Variables in Windows are denoted with percent signs (%) 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:\Linux

C:\>set Penguins=C:\Windows;%Penguins%

C:\>echo %Penguins%
C:\Windows;C:\Linux

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.

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 value should be enclosed in quotes if it contains any spaces. Note that variables may expand to a string with spaces (e.g., %PATH% becomes C:\Program Files), so it is best to include quotes around values 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, which are built into Windows. Some examples are: %WINDIR%, %SystemRoot%, %USERPROFILE%, and %APPDATA%. Like most names in Windows, these are case-insensitive.

 

Unix derivatives (FreeBSD, GNU / Linux, OS X)

Environment Variables in Linux are prefixed with a dollar sign ($) such as $HOME or $HOSTNAME. Many well-known and standard variables are spelled out in capital letters to signify just that. Keep in mind that variable names are case-sensitive, meaning that $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., /etc/profile, $HOME/.bash_profile). The .profile file in the home folder is a common place to define user variables.

Setting variables

These files are regular shell scripts and can contain more than just environment variable declarations. To set an environment variable, use export. To show your currently defined environment variables in a terminal, run env.

The export command is a standard way to define variables. The syntax is very intuitive. The outcome is identical for these two lines, but the first alternative is preferable in case portability to pre-POSIX Bourne shell is necessary.

var=value; export var
export var=value

The C shell and its descendants use a completely different syntax; there, the command is setenv.

See the Linux documentation project, Path HOWTO for a more thorough discussion on this topic.

Perhaps contrary to common belief, OS X is more "Unix" than Linux. 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.

 

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
TLDP: Path Howto

Daniel Beck

Posted 2011-05-16T18:25:57.433

Reputation: 98 421

@Daniel What do you mean by "note that if %PATH% is included, there will be spaces"? – Pacerier – 2014-11-12T00:16:01.830

@Pacerier This answer was collaboratively edited, and that section not written by me. That said I just checked and it seems %PATH% by default contains entries referring to folders in C:\Program files, meaning that it evaluates to a string with spaces that needs proper quoting. – Daniel Beck – 2014-11-12T06:19:25.250

@DanielBeck, Ok, I've edited it. – Pacerier – 2014-11-13T20:34:32.570

@Pacerier I don't think that's right. There is no sane configuration where PATH equals C:\Program files. – Daniel Beck – 2014-11-13T21:45:02.490

@DanielBeck, Why not? I'm not experiencing any problems. – Pacerier – 2014-11-14T08:30:58.103

Quotes are needed when the argument for Windows set command contains spaces. Both following commands will work, the first will make the variable contain the quotes: set FunnyCatPictures="C:\Users\Daniel\Pictures\Funny Cat Pictures" and the second not: set "FunnyCatPictures=C:\Users\Daniel\Pictures\Funny Cat Pictures" – Melebius – 2015-02-09T05:58:23.573

@tripleee I'm not convinced that your edit is correct. "export VAR=value" works for me in dash, and the POSIX man page for export shows the syntax as export name[=word]. Can you provide a reference supporting your claim?

– G-Man Says 'Reinstate Monica' – 2015-03-28T01:08:42.933

@G-Man I can now -- thanks for asking (-: Edit suggestion submitted.

– tripleee – 2015-03-28T17:46:00.977

10On Windows Vista onwards you can use SETX to make permanent changes. It has a slightly different syntax. SETX has to be added from the Windows Server 2003 Resource Kit for earlier versions of NT. – paradroid – 2011-05-16T23:58:48.570

1@paradroid Nice, didn't know that (not a Windows user). This topic is community wiki, you can go ahead and just edit in that information. – Daniel Beck – 2011-05-17T04:05:48.827

What you describe as "Shell" are not environment variables. Nor do they apply solely to Windows Explorer. That is an environment variable template, which any program that understands the protocol can participate in using. See this ServerFault answer.

– JdeBP – 2011-05-24T07:11:10.893

@JdeBP I don't understand how this applies here, as the user mentioned it didn't work with editing the registry, but did work when editing using the dialog. What's your point? – Daniel Beck – 2011-05-24T08:07:26.593

The point is fairly clearly made. Here it is in boldface: These are not environment variables, merely a template from which environment variables are constructed, and they are not confined solely to Microsoft Windows Explorer (the "shell"). – JdeBP – 2011-05-24T11:15:00.853

2@JdeBP The section title was meant to reference how to set these values on the user interface, which is called shell on Windows, in contrast to the section following, command-line interface. I edited this before I even saw your comment 10 hours ago. Noone ever mentioned that this applied only to Explorer. – Daniel Beck – 2011-05-24T18:15:57.380

1@JdeBP The answer you linked to doesn't really apply in this case, since you talked about setting the values in the registry. Setting via dialog will broadcast that WM_SETTINGCHANGE I imagine, and therefore notify interested parties about the new values. That's why I asked how it applied here. Making something boldface doesn't change its meaning and neither makes it easier to understand, just like repeating the same sentence, only louder. Also, I edited this post a few hours ago to specifically mention that the values the processes receive are set when they are launched. – Daniel Beck – 2011-05-24T18:19:42.147

3@JdeBP You might also notice that the question is rather basic in nature. Carefully explaining the intricacies of how changes to the environment variables are distributed (seriously, WM_SETTINGCHANGE? Windows API? This isn't this question's audience!) might not be a good idea, as it might confuse the reader instead of explaining what these variables are about. – Daniel Beck – 2011-05-24T18:23:13.047

1@muntoo Thanks for working on this answer. I have to ask though about the wording It specifies the directories in which executable programs are located on the machine. Programs that are located in the directories specified by PATH can be started without knowing and typing the whole path to the file on the command line. — any reason you changed it like you did (aside from grammar issues)? Programs can be located pretty much anywhere, the path only is used for locating; so I disagree with how the first sentence turned out. – Daniel Beck – 2011-05-25T06:25:53.480

@Daniel I was trying to 'chop' it into shorter sentences (what every English teacher seems to advocate), but I accidentally introduced that mistake. (Fixed, now.) I've also added a suggested 'For example' which might make it easier to understand. It's a bit redundant, though. Maybe you can help integrate it into the post a bit better? – Mateen Ulhaq – 2011-05-25T07:24:43.360

1I'd say it's a very elaborate answer that will surely help many people, thanks to all who helped to write it. I don't think we should go into Windows' internals either, that's not what I intended with the question anyway. – slhck – 2011-05-25T07:41:20.560

I don't think either Rapid Environment Editor or Path Editor are open source. RapidEE license, no license for Path Editor. Perhaps you were thinking of PathEd ?

– Ehtesh Choudhury – 2013-09-13T21:18:08.310

64

This post is from a more technical point of view than Daniel's, but doesn't explain as much the concepts.


The Wikipedia article is also an excellent referrence.

Linux and most BSDs

In most command-line shells, temporary environment variables are set using export (sh, bash, zsh) or setenv (csh, tcsh) commands.

  • Examples for prepending $HOME/bin to $PATH in bash or zsh:

    export PATH="$HOME/bin:$PATH"
    

    (In this particular case, export is unnecessary since PATH is already part of the environment.)

  • csh, tcsh:

    setenv PATH "$HOME/bin:$PATH"
    

Persistent environment variables can be set during several separate stages:

  • Login:

    • Login session setup: Linux pam_env, which usually reads from /etc/environment

    • Terminal logins: The shell's "profile" files.

      • bash uses /etc/profile and the first one of: ~/.bash_profile, ~/.bash_login, ~/.profile. Manual page bash(1) section Invocation.

        Often, the user's profile file includes an explicit call for ~/.bashrc too.

      • zsh: Manual page zsh(1) section Startup/shutdown files.

      • csh and other shells: See apropriate manual pages.

    • Graphical logins: Not sure; may vary depending on login manager. GDM appears to read ~/.profile in my system.

  • Opening of a terminal window:

    • bash uses /etc/bash.bashrc and ~/.bashrc.

Windows NT series

  • In Command Prompt (cmd.exe), use set name=value to change environment variables for that window.

    To append c:\bin to %PATH%, use:

    set path=%path%;c:\bin
    

    This only affects that cmd.exe process, and any new processes launched from it.

  • To make persistent changes, use Control Panel → System → Advanced → Environment Variables. (docs)

    Note: While user settings normally override system ones, PATH variable is handled specially: both system and user settings are merged into the final value.

    Changes are stored to Registry (see below), and apply instantly to all new processes created by Explorer (the graphical shell), for example, through Start Menu.

  • System-wide environment variables are kept in the Registry, HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment, and loaded at system boot.

    User environment is kept in HKCU\Environment and loaded during user logon.

  • By default, the system also looks for set commands in AUTOEXEC.NT file.

MS-DOS, 16-bit Windows, Windows 9x series

  • In MS-DOS Prompt (command.com), use set name=value, as in WinNT.

    Similarly, running set interactively only affects that one command.com instance, along with any new processes launched from it.

  • To make persistent changes, add or edit apropriate set lines in C:\AUTOEXEC.BAT, then reboot.

    • Workaround to avoid restarting, for Windows 9x: open a MS-DOS Prompt window, set the apropriate variables, close existing Explorer process (Start → Shut Down → while holding Ctrl+Shift, click Cancel), run explorer.exe from the MS-DOS Prompt window.

VMS

(Couldn't resist.)

DCL has no concept of "path", but various symbol and command definitions can be put in SYS$LOGIN:LOGIN.COM.

user1686

Posted 2011-05-16T18:25:57.433

Reputation: 283 655

2

The VMS section is wrong. DCL has DCL$PATH for "automatic foreign commands".

– JdeBP – 2011-05-24T07:17:20.447

9

For the bash shell PATH global (non-terminal) environment variables, I follow the convention used in my Ubuntu VM installation - other shells will vary:

Caveat: The whole shell start-up sequence, which .bashrc, .profile, etc. files are sourced in which order?, when do I have to re-login to get visibility to newly defined variables, aliases, etc?, what's the difference between a login, interactive, and non-interactive shell - I do use cron?, and why when I do a . ~/.bashrc is my stupid PATH variable growing longer and longer? are the key questions that come to mind when I'm thinking of my PATH variable.

In fact, I just completely re-wrote my entire bash startup file set taking ideas from the Ubuntu and cygwin skeleton files, and here are some of my in sites:

  1. Export the PATH and other global environment variables (i.e. LD_LIBRARY_PATH) variables in the .profile;
  2. Use logic in ~/.profile to source $HOME/.bashrc if it exists;
  3. Fence execution of the ~/.bashrc with a test for interactive execution, exit otherwise;
  4. Put all the aliases, shopt's, prompt setup, history control, terminal setup, function definition, etc. (interactive related setup) in the part of ~/.bashrc that is protected to only run in interactive mode;
  5. Get rid of the other bash startup files, because their existence determines whether the control path through .profile and .bashrc works as expected. That is, unless there are specific requirements to do otherwise, remove ~/.bash_profile & ~/.bash_login;
  6. When I'm at the bash prompt, and I need to update some default setup, I edit my ~/.bashrc file, then simply source it with a . ~/.bashrc to get those changes in my current shell.
  7. When I make a change to an environment variable like PATH, I need to modify and source my ~/.profile;
  8. I put my calls to fink, port, and brew specific setups in .profile.

That's my 2 cents on this topic.

Billy McCloskey

Posted 2011-05-16T18:25:57.433

Reputation: 1 487

8

Q: WHAT are Environment Variables ?

A: Environment Variables are similar to variables in any programming language. In the case of Windows or Unix systems they are storing various values to allow for programs and tasks to get necessary OS information or 'Environment' information. For example:

USERPROFILE : users directory within the OS files.

MAIL : where a user's mail can be found within the OS files.

Q: WHAT is the PATH variable specifically?

A: The PATH variable sets directory paths to look in when commands are executed, both for RUN commands, and for internal calls from programs. This prevents a program from needing to know its install location to call other executable processes.

It looks to the Windows Environment System PATH variable and tests each location for the given executable. Thus adding a location to the PATH variable allows an executable to be called directly.

According to this Wikipedia article:

When a command is entered in a command shell or a system call is made by a program to execute a program, the system first searches the current working directory and then searches the path, examining each directory from left to right


Q: HOW to add a location to the PATH variable ?

A: You need to edit the variable string of the Environment Variables PATH variable to include your executable's location.

One way to do this is described here:

  1. Open the Start Menu and right click on Computer. Select Properties.
  2. Select Advanced system settings.
  3. In the Advanced tab, select Environment Variables.
  4. Select EDIT or NEW. (for PATH you most likely want to EDIT).
  5. Add your location path. e.g.: C:\wamp\bin\php\php5.5.12;

I hope this clarifies some of the confusion.

user945389

Posted 2011-05-16T18:25:57.433

Reputation: 111

0

Windows 10, without admin account

Control Panel -> User Accounts -> User Accounts -> Change my environment variables

Note you have to click on the header "User Accounts": Note you have to click on the header "User Accounts"

Again, you have to click on the header "User Accounts": Note again you have to click on the header "User Accounts"

Change my environment variables

Environment Variables

Raf

Posted 2011-05-16T18:25:57.433

Reputation: 131