Batch Character Escaping


Answer :

This is adapted with permission of the author from the page Batch files - Escape Characters on Rob van der Woude's Scripting Pages site.

TLDR

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 ;)

Percent Sign %

% can be escaped as %% – "May not always be required [to be escaped] in doublequoted strings, just try"

Generally, Use a Caret ^

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":

  • ,
  • ;
  • =
  • (
  • )

Double Escape Exclamation Points when Using Delayed Variable Expansion

! must be escaped ^^! when delayed variable expansion is active.

Double Double-Quotes in find Search Patterns

"""

Use a Backslash in findstr Regex Patterns

  • \
  • [
  • ]
  • "
  • .
  • *
  • ?

Also

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] 

You could simply use an external file as input for sed.

Or using strings directly in batch, it's a good idea to use the delayed expansion.

setlocal DisableDelayedExpansion set "regEx=s/^#*$/""/g" setlocal EnableDelayedExpansion sed !regEx! file.txt

EDIT: How to use unmodified strings with a batch

This uses findstr to get the string directly from the batch and return it into a result-variable.
So you can use the sed-string as is.

@echo off setlocal REM SedString1#(^.*)(Form Product=")([^"]*") FormType="[^"]*" FormID="([0-9][0-9]*)".*$  call :GetSEDString result SedString1 setLocal EnableDelayedExpansion echo the sedString is !result! sed !result! goto :eof  :GetSEDString <resultVar> <searchName> :: Search the own batch file for <searchName> in a line with "REM <searchName>#" :: Return all after the "#" without any modification setLocal DisableDelayedExpansion for /f "usebackq tokens=* delims=" %%G in (`findstr /n /c:"REM %~2#" "%~f0"`) do (     set "str=%%G" ) setLocal EnableDelayedExpansion set "str=!str:*#=!"  for /F "delims=" %%A in ("!str!") DO (   endlocal   endlocal   set "%~1=%%A"   goto :eof )  goto :eof 

Comments

Popular posts from this blog

Are Regular VACUUM ANALYZE Still Recommended Under 9.1?

Can Feynman Diagrams Be Used To Represent Any Perturbation Theory?