CMD enviroment variables not expanded in PATH vari

2019-06-06 01:27发布

I've some enviroment variables set in my sistem preferences (Windows 8.1). If i start cmd.exe and execute this commands:

C:\Users\Carlo>echo %GRAILS_HOME%
D:\Grails

C:\Users\Carlo>echo %GRADLE_HOME%
D:\Gradle

C:\Users\Carlo>echo %GROOVY_HOME%
D:\Groovy

C:\Users\Carlo>echo %PATH%
C:\ProgramData\Oracle\Java\javapath;c:\Program Files (x86)\Intel\iCLSClient\;
c:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;
C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;
C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;
C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;
C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;
C:\Program Files (x86)\Brackets\command;%GRAILS_HOME%\bin;%GRADLE_HOME%\bin;
%GROOVY_HOME%\bin

Why the last three variables is not expanded in PATH?

Complete PATH variable is:

C:\ProgramData\Oracle\Java\javapath;c:\Program Files (x86)\Intel\iCLS Client\;c:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Brackets\command;%GRAILS_HOME%\bin;%GRADLE_HOME%\bin;%GROOVY_HOME%\bin

1条回答
唯我独甜
2楼-- · 2019-06-06 02:16

Environment variables (EnvVars) are simply what their name state: variables; They have a name and a value (in Windows the value is a string).

So in your example the name is PATH and the value is the string you listed. The OS (command interpreter) doesn't have any idea about the semantics of the EnvVar value so when it expands the var name it just shows its value. It doesn't analyze the value and recursively expand any other variables that may be there.

If variables are defined in other variables (something that I wouldn't recommend) is the consumer's job to expand all of them.

A common C function that deals with EnvVars (maybe this will help): ExpandEnvironmentStrings

查看更多
登录 后发表回答