The enigma of the missing caret

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

The enigma of the missing caret

Post by MigrationUser »

23 Nov 2010 00:17
jeb

Hi,

perhaps someone can explain this simple and obvious example ;)

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "singleCaret=^"
set "^=One caret"
set "^^=Two carets"
call echo %%!singleCaret!%%
--- output ---
Two carets

Why you get "two carets" instead of "One caret"?
And why it doesn't work with call echo %%%singleCaret%%%?

hope you have fun

jeb

Last edited by jeb (23 Nov 2010 00:21)

----------------------------

#2 23 Nov 2010 06:25
jumper

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "singleCaret=^"
set "^=One caret"
set "^^=Two carets"
%ComsPec% /v:on /c echo %%!singleCaret!%%

--- output ---
One caret

Last edited by jumper (23 Nov 2010 06:26)

----------------------------

#3 24 Nov 2010 21:18
jeb


The question is:

Can you explain the logic behind?

echo %^% -- also displays One Caret
But why not
call echo %%!singleCaret!%%
and this one breaks the rules, or what?
%ComsPec% /v:on /c echo %%!singleCaret!%%

jeb

Last edited by jeb (24 Nov 2010 21:19)

----------------------------

#4 16 Apr 2012 02:22
dbenham


jeb - how can you leave everyone hanging like that? :twisted: :D

I know you now the answer. I'll explain it for the benefit of everyone else (as if they care ;) ).

The odd behavior is due to caret doubling that occurs whenever a CALL is made.

Normally unquoted carets are doubled by the call, and reduced back to a single caret by the special character handling phase of the parser. Quoted carets remain doubled after CALL because the special character phase ignores quoted special characters.

But in your case, the percent variable expansion occurs after the CALL but prior to the special character handling phase. So the expansion phase sees the two carets and expands accordingly.

The %comspec% version never uses CALL, so the carets are never doubled, and you get your one caret answer.

Case solved
Post Reply