Mystery: Carets

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeb
Expert
Posts: 1058
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Mystery: Carets

#1 Post by jeb » 08 Sep 2010 03:33

Can someone explain this? :)

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "^=one"
set "^^=two"
set "^"
set "caret=^"
call echo I have %%!caret!%% carets, but why?


You got

Code: Select all

^=one
^^=two
I have two carets, but why?


make a guess ...


jeb

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Mystery: Carets

#2 Post by orange_batch » 08 Sep 2010 05:20

jeb wrote:

Code: Select all

I have two carets, but why?


Because you touch yourself at night. :lol:

Are you asking for yourself, or to see if anyone knows? Either way, the command processor seems to double up the "string" caret before execution. No way around this...

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

Re: Mystery: Carets

#3 Post by jeb » 08 Sep 2010 08:54

I know the answer. :wink:

You are near, but as you can see the solution is not so simple.

Code: Select all

setlocal EnableDelayedExpansion
set varE=^^!varE^^!#
set varP=%%varP%%#
set "caret=^"
call echo !caret!


You got only one ^

miskox
Posts: 649
Joined: 28 Jun 2010 03:46

Re: Mystery: Carets

#4 Post by miskox » 08 Sep 2010 14:34

Let me try it.

I will use BASIC, maybe in this way it will be easier to understand:

10 set ^="one"
20 set ^^="two"
30 set ^="" : rem Deletes variable ^
40 set caret=^ : rem variable caret gets the value of variable ^
50 print "..."

In line 50 variable from line 20 gets processed which is single caret because it is escaped by another caret.

Am I right?

Saso

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Mystery: Carets

#5 Post by aGerman » 08 Sep 2010 16:40

!caret! is expanded to ^. The CALL command doubles the ^. So you have %^^% and this is expandet to two.
_____________________________________________________________________________

jeb wrote:You got only one ^

Hmm, I think you got
echo ^^
and this results to a single caret.

Regards
aGerman

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Mystery: Carets

#6 Post by ghostmachine4 » 08 Sep 2010 19:03

that's one of the problem with DOS. Caret (or other special characters ) should not be allowed to be used as variable names.

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

Re: Mystery: Carets

#7 Post by jeb » 09 Sep 2010 00:35

aGerman wrote:!caret! is expanded to ^. The CALL command doubles the ^. So you have %^^% and this is expandet to two.


Yes this it is, but ...

Code: Select all

echo ^ zero
echo ^^ one caret
call echo !caret! one caret
call echo ^^ one caret
call echo ^^^^ two carets "^^^^" four carets
rem BUT NOW
call echo ^^^^ two carets "^^^^" eight carets (oho)
call call echo ^^^^ two carets "^^^^" sixteen carets (oh oh)


Each call doubles all carets in a line, also in quotes.
Normally, the carets are escaping themself, so a double caret is shrinking to one, but this does not work in quotes.
And as the escape-phase is after the % expanding phase you get the result of %%!caret!%% as
%^^%

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Mystery: Carets

#8 Post by orange_batch » 09 Sep 2010 12:19

Haha jeb, a real DOS scientist. Bravo.

Post Reply