4

On my Windows 7 computer, I created some custom user environment variables (right-click computer > properties > "Advanced system settings" > "Environment Variables..." > "New..." for "User variables for ...") but when I echo them in command prompt it seems to not recognize one. For example, this is what I had:

UTILS_HOME  C:\myUtils
UTILS_WILDFLY  %UTILS_HOME%\wildflyUtils

I added both to the PATH variable in my user variables like this:

PATH  %UTILS_HOME%;%UTILS_WILDFLY%

When I echo the PATH I get this:

C:\mytils;%UTILS_WILDFLY%

I expect that it would expand UTILS_WILDFLY but doesn't. Now if I change the name of UTILS_WILDFLY to JBOSS_8, when I echo the path it'll get expanded. I've tried several names for testing and do not understand why some expand and why some do not. When they do not expand I'm not able to access my scripts in that folder on the command line.

Is there some rule that I don't know about for naming environment variables or is this the way it works and I have to do trial and error until I find one that does work?

There are no typos when trying different names. I created the name for the variable and cut and paste it in the PATH variable to rule that out.

Chuck L
  • 43

2 Answers2

6

The problem is that you're using recursive expansion, i.e., PATH references UTILS_WILDFLY which itself references UTILS_HOME.

Recursive expansion doesn't always work; presumably that means that it isn't supported, so there's no guarantee that it will ever work - which means you shouldn't use it - but in practice it does work sometimes, hence the confusion.

Specifically, on Windows 7, it works if and only if the variable in the middle of the recursive expansion (UTILS_WILDFLY) appears before the variable being expanded (PATH) in the list of variables in the registry. As it happens, the environment variables are alphabetized.

In your case,

  • UTILS_WILDFLY > PATH, so that doesn't work.

  • JBOSS_8 < PATH, so that does.

PATI won't work, but PATG will.

Harry Johnston
  • 5,914
  • 8
  • 34
  • 58
0

I've also encountered such question. Although this question is done, I'd like to give a quick workaround for future users.

In my situation, I've created two env variables JAVA_HOME and GRADLE_HOME.

D:\>echo %JAVA_HOME%
D:\Program Files\Java\jdk1.8.0_121

D:\>echo %GRADLE_HOME%
D:\Program Files\gradle-3.5

Then %JAVA_HOME%\bin and %GRADLE%_HOME\bin to Path. But by echo %Path%,

D:\Program Files\Java\jdk1.8.0_121\bin;D:\Program Files\Java\jdk1.8.0_12 1\jre;%GRADLE_HOME%\bin;%GROOVY_HOME%\bin; Same situation.

I've also check the reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v * suggested by @eryksun, the result is alphabetic order. So the answer for the root cause for this question may not be windows expands env var in alphabetic order.

My workaround,

GRADLE_HOME -> GGRADLE_HOME then, change back. GGRADLE_HOME -> GRADLE_HOME

Hope someone proficient in windows can give a explanation for such kind of issues.

caisil
  • 101
  • 1