7

I'm trying to echo a string stored in a variable but it seems to require a lot of escaping. I'm trying with the following code:

setlocal EnableDelayedExpansion
@echo off
set "grass=@##&!$^&%**(&)"
echo !grass!

I want to echo the variable grass verbatim so I see a @##&!$^&%**(&) in the output. What should be done? Thanks!

user1077213
  • 209
  • 2
  • 4
  • 6

2 Answers2

16

echo !grass! will always echo the current value verbatim, without the need of any escaping. Your problem is, the value is not what you think it is! The problem is occurring when you are trying to SET the value.

The correct escape sequence to set your value is

set "grass=@##&^!$^^&%%**(&)"

And now for the explanation. The information you need is buried in How does the Windows Command Interpreter (CMD.EXE) parse scripts?. But it is a bit hard to follow.

You have two problems:

1) % must be escaped as %% for each time the line will be parsed. The presence or absence of quotes makes no difference. The delayed expansion state also makes no difference.

 set pct=%
 :: pct is undefined

 set pct=%%
 :: pct=%

 call set pct=%%
 :: pct is undefined because the line is parsed twice due to CALL

 call set pct=%%%%
 :: pct=%

2) A ! literal must be escaped as ^! whenever it is parsed by the delayed expansion phase of the parser. If a line contains ! anywhere within it during delayed expansion, then a ^ literal must be escaped as ^^. But the ^ must also be either quoted or escaped as ^^ for the special character phase of the parser. This can be further complicated by the fact that a CALL will double up any ^ characters. (Sorry, it is very difficult to describe the problem, and the parser is complicated!)

setlocal disableDelayedExpansion

set test=^^
:: test=^

set "test=^"
:: test=^

call set test=^^
:: test=^
:: 1st pass - ^^ becomes ^
:: CALL doubles ^, so we are back to ^^
:: 2nd pass - ^^ becomes ^

call set "test=^"
:: test=^^ because of CALL doubling. There is nothing that can prevent this.

set "test=^...!"
:: test=^...!
:: ! has no impact on ^ when delayed expansion is disabled

setlocal enableDelayedExpansion

set "test=^"
:: test=^
:: There is no ! on the line, so no need to escape the quoted ^.

set "test=^!"
:: test=!

set test=^^!
:: test=!
:: ! must be escaped, and then the unquoted escape must be escaped

set var=hello
set "test=!var! ^^ ^!"
:: test=hello ^ !
:: quoted ^ literal must be escaped because ! appears in line

set test=!var! ^^^^ ^^!
:: test=hello ^ !
:: The unquoted escape for the ^ literal must itself be escaped
:: The same is true for the ! literal

call set test=!var! ^^^^ ^^!
:: test=hello ^ !
:: Delayed expansion phase occurs in the 1st pass only
:: CALL doubling protects the unquoted ^ literal in the 2nd pass

call set "test=!var! ^^ ^!"
:: test=hello ^^ !
:: Again, there is no way to prevent the doubling of the quoted ^ literal
:: when it is passed through CALL
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

As dbenham said, it's only the assignment.
You could use the escaping like dbenham showed, it's necessary as EnableDelayedExpansion is active while you set the value.
Therefore you need to escape the exclamation mark and the caret must be escaped as there is one exclamation mark in the line, quotes are useless in that situation.
But you could also set the value before you activate the EnableDelayedExpansion,
Then you didn't need the carets.

jeb
  • 78,592
  • 17
  • 171
  • 225
  • So for example, how would the escaped form of `@##&!$^&%**(&)` look like if you set the value before enabling delayed expansion? – user1077213 Mar 20 '12 at 07:43
  • 2
    @user1077213 - As long as the value is quoted, then only the % needs to be doubled: `set "grass=@##&!$^&%%**(&)"`. Without the quotes you need many escapes: `set grass=@##^&!$^^^&%%**(^&)` will work as long as is is not within a parenthesized block. If parentheses are active, then the closing `)` must also be escaped: `set grass=@##^&!$^^^&%%**(^&^)`. – dbenham Mar 20 '12 at 14:30