Windows – Are there rules to Windows 7 Environment variable names

environment-variableswindowswindows 7

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.

Best Answer

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.

Related Question