Page 1 of 1

NEW BUG: DOUBLING CARET CHARACTERS.

Posted: 03 Aug 2015 09:39
by OperatorGK
Code below:

Code: Select all

@echo off
echo "^"
call echo "^"
call call echo "^"

produces following results for me:

Code: Select all

"^"
"^^"
"^^^^"

Any suggestions?

Re: NEW BUG: DOUBLING CARET CHARACTERS.

Posted: 03 Aug 2015 15:50
by foxidrive
Just commenting that it's a known problem with calling a subroutine and a caret is in the parameter list.
The call keyword is the problem.

See here:

Code: Select all

@echo off
echo "^"
call :next  "^"
call :next  "^^"
pause
goto :EOF

:next
echo %*

Re: NEW BUG: DOUBLING CARET CHARACTERS.

Posted: 04 Aug 2015 04:15
by jeb
Hi OperatorGK,

it's not a bug, it's the well known behaviour of the call command (well known but not documented) :)

1. A call always doubles all carets in the line as the first phase by the parser.
Then only three more parser phases are executed.

2. The percent expansion.
3. The removing of all CR characters.
4. The special character phase where carets, ampersand, redirections are detected.
Ignored phases are:
ECHO ON phase
Delayed Expansion phase
FOR-LOOP-parameter expansion

The 4th phase also detects FOR, IF and REM and uses a different parser for those:

The 4th phase has many problems when invoked by a CALL.
The parsing/executing of FOR,REM,IF always fails.
Redirections are parsed but completly ignored.
When pipes |, multiple commands &, || or && are present, then it fails silently and nothing will be executed, also for brackets.

With the special characters and brackets you can create some strange other effects, ex.

Code: Select all

CALL (echo ha | echo hu) 

Will execute the file 2.bat :!:
More about this at CALL me, or better avoid call

More about the batch parser at SO: How does the Windows Command Interpreter (CMD.EXE) parse scripts?

And some examples

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "singleCaret=^"
set "^=one caret"
set "^^=two carets"
echo #1 !singleCaret!
call echo #2 !singleCaret!
call call echo #3 !singleCaret!
call call call echo #4 !singleCaret!
echo #5 %%!singleCaret!%%
call echo #6 %%!singleCaret!%%

jeb

Re: NEW BUG: DOUBLING CARET CHARACTERS.

Posted: 04 Aug 2015 09:24
by OperatorGK
CMD.EXE never ceases to amaze me :D. Time to revisit some of my code, happily I never used call frequently.