I'm fairly proficient at writing Batch scripts for Windows, but even after all these years how to correctly escape characters puzzles me. It's especially difficult when trying figure out the correct way to escape a regular expression for use with sed. Is there any tool that can help me? Perhaps something that allows me to paste in a "normal" string and it spits out the correctly escaped version of that string?
Update: I'm reluctant to give an example because I'm not looking for an answer on how to escape one specific string. I'm also not looking for a solution that will work for one specific app. I'm looking for a tool that will help me get the escape syntax correct for every string I ever need to escape no matter what tool might be consuming it from the command line.
That being said the regex I really want is
(^.*)(Form Product=")([^"]*") FormType="[^"]*" FormID="([0-9][0-9]*)".*$
Take that true regex (i.e. unescaped as far as BATCH is concerned) and wrap it in some sed syntax such as ssed "s@ --- Insert escaped regex here --- @http://psph/\1/\2@g" "%~1"
and finally escape it... Again, is there any tool that can assist in escaping any string for use on the BATCH command line?
p.s. There are so many exceptions to BATCH's escaping syntax that I'll even settle for a good cheat sheet.
In batch files, the percent sign may be "escaped" by using a double percent sign ( %% ). That way, a single percent sign will be used as literal within the command line, instead of being further interpreted.
In computing and telecommunication, an escape character is a character that invokes an alternative interpretation on the following characters in a character sequence. An escape character is a particular case of metacharacters.
In the Command Prompt, the caret character ( ^ ) will let you escape spaces—in theory. Just add it before each space in the file name. (You'll find this character in the number row on your keyboard. To type the caret character, press Shift+6.)
batch-file Escaping special characters Escape using caret(^) Most special characters can be escaped using the caret( ^ ).
As dbhenham points out in this comment, a (MUCH) more detailed answer can be found in portions of this answer (originally by another user jeb and significantly edited and updated by dbhenham since) on a related but much more general question:
Note that, per dbhenham, this answer is:
incorrect, misleading, and incomplete
I think this answer is still good enough, for almost all cases, but a careful reading of the above answer might be warranted depending on one's exact character escaping needs and the limitations of this answer.
The remaining has been adapted with permission of the author from the page Batch files - Escape Characters on Rob van der Woude's Scripting Pages site.
Windows (and DOS) batch file character escaping is complicated:
Much like the universe, if anyone ever does fully come to understand Batch then the language will instantly be replaced by an infinitely weirder and more complex version of itself. This has obviously happened at least once before ;)
%
%
can be escaped as %%
– "May not always be required [to be escaped] in doublequoted strings, just try"
^
These characters "may not always be required [to be escaped] in doublequoted strings, but it won't hurt":
^
&
<
>
|
Example: echo a ^> b
to print a > b
on screen
'
is "required [to be escaped] only in the FOR /F
"subject" (i.e. between the parenthesis), unless backq
is used"
`
is "required [to be escaped] only in the FOR /F
"subject" (i.e. between the parenthesis), if backq
is used"
These characters are "required [to be escaped] only in the FOR /F
"subject" (i.e. between the parenthesis), even in doublequoted strings":
,
;
=
(
)
!
must be escaped ^^!
when delayed variable expansion is active.
find
Search Patterns"
→ ""
findstr
Regex Patterns\
[
]
"
.
*
?
Rob commented further on this question (via email correspondence with myself):
As for the answer, I'm afraid the chaos is even worse than the original poster realizes: requirements for escaping parentheses also depend on the string being inside a code block or not!
I guess an automated tool could just insert a caret before every character, then doubling all percent signs - and it would still fail if the string is doublequoted!
Further, individual programs are responsible for parsing their command line arguments so some of the escaping required for, e.g. for sed
or ssed
, may be due to the specific programs called in the batch scripts.
The escape character for batch is the caret (^
). If you want to include any of the pipeline characters in your script you need to prefix the character with the caret:
:: Won't work: @echo Syntax: MyCommand > [file] :: Will work: @echo Syntax: MyCommand ^> [file]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With