2

Whats's going on?

helper.bat

@echo off
echo %1
call:foo %1
goto:eof

:foo
echo %1
goto:eof

Run our script like the following

helper "^^^^"

Output

"^^^^"

"^^^^^^^^"

Why? I know that '^' symbol is smth special in case of cmd.exe, but what's going on here? How the function call affect on it?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

3

CALL is very special in this case!

The batch parser has different phases, in the special character phase unquoted carets are used to escape the next character, the caret itself is removed.
In your case, the carets are quoted, so they will not be affected.

Then the carets can be affected again in the delayed expansion phase, but quotes havn't special meaning there, the carets are used only to escape exclamation marks.

Normally after the delayed phase all is done, BUT if you use CALL all carets are doubled.
Normally this is invisible, as the CALL also restarts the parser and carets are removed in the special character phase again.
But in your case they are quoted, therefore they stay doubled.

Try this

call call call call echo a^^ "b^"

Output

a^ "b^^^^^^^^^^^^^^^^"

The parser is explained at How does the Windows Command Interpreter (CMD.EXE) parse scripts?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225
  • Is it documented behavior to use smth like "call :foo %%1" instead of "call :foo %1"? – FrozenHeart Jul 17 '13 at 17:21
  • Yes, it's the standard technic to get one more parse phase, often useful in parenthesis, like `call set line=%%line:abc=def%%` – jeb Jul 17 '13 at 18:07
  • So, i can use this trick to avoid duplication of '^' symbols? Do you know where can i read about it in MSDN? – FrozenHeart Jul 17 '13 at 18:30
  • Yes you can use that trick. Nowhere at MSDN, it is doubtful that Microsoft still know this. – jeb Jul 18 '13 at 06:40