NEW BUG: DOUBLING CARET CHARACTERS.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

NEW BUG: DOUBLING CARET CHARACTERS.

#1 Post by OperatorGK » 03 Aug 2015 09:39

Code below:

Code: Select all

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

produces following results for me:

Code: Select all

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

Any suggestions?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: NEW BUG: DOUBLING CARET CHARACTERS.

#2 Post by foxidrive » 03 Aug 2015 15:50

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 %*

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: NEW BUG: DOUBLING CARET CHARACTERS.

#3 Post by jeb » 04 Aug 2015 04:15

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

OperatorGK
Posts: 66
Joined: 13 Jan 2015 06:55

Re: NEW BUG: DOUBLING CARET CHARACTERS.

#4 Post by OperatorGK » 04 Aug 2015 09:24

CMD.EXE never ceases to amaze me :D. Time to revisit some of my code, happily I never used call frequently.

Post Reply