Jay Taylor's notes
back to listing indexCertain Variables won't expand in Windows System Environment Variables PATH - Super User
[web search]-
Teams
Now available on Stack Overflow for Teams! AI features where you work: search, IDE, and chat.
Learn more Explore Teams -
Looking for
For some reason there are certain Environment Variables that won't expand when used in the "PATH" variable in the System Variables.
For example, %SystemRoot%
works just fine while %WinDir%
doesn't. %ProgramFiles%
and %ProgramFiles(x86)%
doesn't work either.
Obviously I know that I could just use the full path rather than a variable but that's not the point.
Any ideas?
Here is a screenshot/example:
-
What Windows OS is this (e.g. 7, 8, 10, XP, etc.) and have you already restarted your PC just in case it's a one-time fluke, memory fragmentation, or some update pending, etc. type of an issue? Commented Feb 1, 2016 at 3:43
-
1 Answer
SystemRoot
expands as expected because it is a pseudo/predefined environment variable. WinDir
is a regular environment variable, and "competes" with others like PATH
in the initialization sequence.
Best explained by Raymond Chen at Windows Confidential: The hidden variables: "Embedding one environment variable within another is simply a matter of good operational timing". Quoting more:
Here’s how the environment-building process works. It proceeds in roughly four steps:
- First, the system creates some predefined machine-wide environment variables, such as SystemRoot and ALLUSERSPROFILE (but not COMPUTERNAME or ProgramFiles).
- Second, it creates environment variables from the System section of the Environment Variables dialog box. The System environment variable definitions can use the “%” notation to refer to the predefined environment variables created in the previous step. For example, you can set a System environment variable to %SystemDrive%Extras. After the System environment is complete, Windows starts building the User environment.
- Step three is to create predefined per-user environment variables, such as USERPROFILE and APPDATA. The COMPUTERNAME and ProgramFiles-related variables are also created here, even though they’re technically System variables and not per-user variables.
- Finally, the system creates the environment variables. These are in the User section of the Environment Variables dialog box and have access to any variables created by the first three steps, so you can set a User environment variable to %USERPROFILE%Extras or a custom System environment variable set in the second step. If a User environment variable has the same name as a System environment variable, the new value replaces the old.
...
One customer was having difficulty setting the System PATH environment variable to %APPDATA%;C:Windows. They found the final environment merely contained the literal path as specified (percent signs and all), instead of replacing it with the value of the APPDATA environment variable. If you look through the sequence of operations previously detailed, it’s clear why this occurred. They were trying to set a System environment variable based on a variable that had not yet been defined.
The solution was simple: Move editing the PATH from the System environment list box to the User environment list box. That way, when it wanted to use the %APPDATA% environment variable, the variable would be there.
For a simple example of possible "race conditions" when defining environment variables based on others, consider the circular case where one defines two system variables as:
bbb=%ccc%
ccc=%bbb%
On my Windows 7 this results in the variables evaluating to:
C:etc>set
...
bbb=%ccc%
ccc=%ccc%
-
Ahhh that makes sense. Thanks for that explanation! Commented Feb 1, 2016 at 6:05
-
it doesn't make any sense at least in such vague terms like "some predefined variables", "SystemRoot but not ComputerName" and especially ProgramFiles which is technically (sic!) System but in fact isn't. I tried to google any formal specs but there are no any at least at the first 5 screens of google output (– maoizmCommented Apr 29, 2017 at 17:43
-
@maoizm See also ss64.com's Windows Environment Variables and this answer to Why can't I access a variable named CD on Windows 7?.– dxivCommented Apr 29, 2017 at 17:56
-
6I discovered the following empirically when I realized that everything alphabetically after "PATH" was not expanded in the path. Is it worth editing in? -> Additionally, the SYSTEM and USER environment variables seem to be loaded in alphabetical order within a section, so if PATH refers to %JAVA_HOME% and %SCALA_HOME%, and those are defined in the same section, then when PATH is expanded, it will include the correct (expanded) value for JAVA_HOME, but it will include the literal (unexpanded) "%SCALA_HOME%".– shooverCommented Sep 27, 2018 at 23:39
-
@shoover Thanks for the additional tidbit. Still, the exact sequence of evaluation for regular environment variables remains undocumented, so it could change with the next Windows build tomorrow, without MS owing any explanation, and therefore it would be a risky gamble to rely on it.– dxivCommented Sep 28, 2018 at 5:37
-
@shoover - Confirming your discovery. If i create
%PATG%
and%PATI%
and add them both to%PATH%
, the former expands, the latter does not. While @dxiv makes a good point that this may not be the official logic, it seems pretty reliable (and stupid) to me. Commented Oct 24, 2019 at 3:27 -
So the workaround is to set extra variables
%MY_SCALA_HOME%
etc.– shooverCommented Oct 24, 2019 at 4:48 -
FWIW, on Windows 11 (23H2), if the
Path
contains any variable that is alphabetically after Path, then none of the variables get expanded. I've discovered this with having%GIT_BIN%
and%TOOLS%
in thePath
, and none of them expanded. I've removed%TOOLS%
, and the remaining%GIT_BIN%
expanded. Which is... surprising, to say the least, but I can fathom a sequence of events, why/how this is happening. Commented May 7 at 5:20
Not the answer you're looking for? Browse other questions tagged or ask your own question.
- The Overflow Blog
-
- Featured on Meta
-
-
- Hot Meta Posts
-
5
Linked
Related
Hot Network Questions
- Does the changed wording for the arcane ward subclass feature (lvl3) imply that recharging it with the armor of shadows is no longer possible?
- Can I use nouns/adjectives with ...てもいいです(か)?
- Why doesn't Bitcoin PoW algorithm use a chain of lower-difficulty hashes?
- Are the Russians planning a successor of the Soyuz?
- Signal-to-noise ratio in predictive modeling and machine learning
- With UK now moving away from BRP cards to eVisa, how can we prove that we are UK residents while applying for visas for other countries?
- Using ID token for role-base authorization in ASP.NET Core MVC
- What is the 3x3 magic square trick for only 3 numbers?
- Is there an obligation for a Beit Din to explain their psak?
- What would the ideal blade for puncturing pressure armor?
- Count the fish in a school
- In the early solar system, were most orbits highly eccentric?
- Episode of a sci-fi series in which Earth is destroyed at the end
- What mnemonic helps differentiate 瑞士 (Switzerland) from 瑞典 (Sweden)?
- Why does the ZX Spectrum ROM set I register, then wait 24 T-states?
- Help Identifying locked toggle switch's purpose
- Did my decision to decline a full time lecturer position look bad at all?
- Is it reasonable for a PhD student to take a weekday off after having to work on a weekend?
- how to stop Particles from moving after landing the ground
- entertaining programmes or entertainment programmes
- Identifying and finding a charger and disk for the Acer Anyware K386s?
- Generate figure showing curvature of 2d surface
- Setting a box on the stairs
- I'm looking for a French movie about a man who kills all the members of a jury, in a single day