Escape Characters

All DOS versions interpret certain characters before executing a command.
Some well know examples are the percent sign ( % ), and the redirection symbols ( < | > ).

Windows 95/98 and NT, and OS/2 too, also interpret double quotes ( " ) and ampersands ( & ), as shown in the Conditional Execution page.

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 Windows 95/98 and NT, and OS/2 too, redirection symbols may be escaped by placing them between double quotes ( ">" ).
However, the quotes themselves will be passed to the command too, unlike the double percent sign.

Windows NT and OS/2 also allow the use of carets ( ^ ) to escape special characters. Even linefeeds can be escaped this way, as is shown in the Useless Tips page.

If you intend to "nest" commands with escaped characters, you may need to escape the escape character itself too.
In general, that won't make it any easier to read or debug your batch files, however.

Since the introduction of delayed variable expansion a new challenge is to escape exclamation marks, the "delayed" version of the percent sign.
Unlike percent signs, that can be escaped by doubling them, exclamation marks cannot be escaped by adding an extra exclamation mark.
Nor does a caret before the exclamation mark work, unless quoted (i.e. ECHO ^! will fail to display an exclamation mark, whereas ECHO "^!" will display a quoted exclamation mark: "!").
Jaime Ramos sent me this link where the solution can be found: use ^^!.
The trick is that a single caret will be used to escape the exclamation mark in the firt "pass" of command line interpretation, but delayed variable expansion adds a second "pass" where the exclamation mark will be interpreted. If you don't get it, never mind, just remember the double caret before the exclamation mark.

Using FIND to search files or standard output for a string containing doublequotes used to be a pain... I often used FINDSTR instead, in these cases.
Robert Cruz explained to me that in this particular case only you need to escape doublequotes by doublequotes, i.e. a single doublequote must be replaced by 2 doublequotes.
He also sent me this link on the subject.

 

Summary

Escape Characters
Character to be escaped Escape Sequence Remark
% %% May not always be required in doublequoted strings, just try
^ ^^ May not always be required in doublequoted strings, but it won't hurt
& ^&
< ^<
> ^>
| ^|
' ^' Required only in the FOR /F "subject" (i.e. between the parenthesis), unless backq is used
` ^` Required only in the FOR /F "subject" (i.e. between the parenthesis), if backq is used
, ^, Required only in the FOR /F "subject" (i.e. between the parenthesis), even in doublequoted strings
; ^;
= ^=
( ^(
) ^)
! ^^! Required only when delayed variable expansion is active
" "" Required only inside the search pattern of FIND
\ \\ Required only inside the regex pattern of FINDSTR
[ \[
] \]
" \"
. \.
* \*
? \?

 

 


page last uploaded: 23 July 2014, 11:27
Fileaze