My co-author answer to jeb answer to "How is the Windows command interpreter (CMD.EXE) parsing scripts?" explains the behavior.
My companion answer provides the necessary information about how the% extension works in order to fully predict the behavior.
If you save ECHO ON, you can see the result of the extension, and the error message makes sense:
test.bat
@echo on @set "ARGV=" if "%ARGV:~,1%"==":" echo %ARGV% begins with a colon.
- output -
C:\test>test echo was unexpected at this time. C:\test>if "~,1" echo begins with a colon.
The important rules of my answer explaining the result of the extension are:
1) (Percent) Starting from the left side, scan each character for % . If found, then
- 1.1 (escape
% ) ... not relevant - 1.2 (expand argument) ... not relevant
- 1.3 (expand variable)
- Else, if command extensions are disabled, then ... not relevant
- Otherwise, if command extensions are enabled, then look at the next line of characters, breaking up to
% : or <LF> and name them VAR (there may be an empty list). If the VAR breaks to : and the subsequent % character, then include : as the last character in the VAR and a break before % .- If the next character is
% , then replace %VAR% with VAR (replace with nothing if no VAR is defined) and continue scanning - Otherwise, if the following character
: then- If the VAR value is undefined, then remove
%VAR: and continue scanning. - ... the rest does not matter
Beginning with
if "%ARGV:~,1%"==":" echo %ARGV% begins with a colon.
Extending a variable expands all of the following lines to zero, because the variable is undefined:
%ARGV: %"==": %ARGV%
And you are left with:
if "~,1" echo begins with a colon.
It works with expansion delay because the IF statement is parsed before slow expansion (explained in jeb's answer in phase 2)
Everything works from the command line because the extension of the command line variable does not delete the line when the variable is not defined. (poorly explained in the jeb answer at the bottom inside CmdLineParser :, Phase1 (Percent))
dbenham
source share