batch-file Escaping special characters Escape using caret(^)

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Most special characters can be escaped using the caret(^). Take a look at the following example.

echo > Hi
echo ^> Hi 

This first command would not output > Hi because > is a special character, which means redirect output to a file. In this case, the file is named "Hi"

However in the second command, > Hi would be outputted without any issue because the caret(^) tells the > to stop functioning as "redirect output to file" command, now > is just a normal character.


Here's a list of special characters that can be escaped(taken, and edited from Rob van der Woude's page)

CharacterEscaped ResultRemarks
^^^
&^&
<^<
>^>
|^|
\^\
!^^!Only required when DelayedExpansion is on

Escaping the caret

Carets can be stacked up to the escape other carets, consider the following example.

InputOutput
^&&
^^^&^&
^^^^^&^^&

Note: The carets in bold form are escaped.


Security issue

A bit off topic here, but this is very important! An unwanted caret escape at the end of the file could cause a memory leak!

any-invalid-command-you-like-here ^

This command would leak all the memory, rendering the system completely unusable! See here for more information.



Got any batch-file Question?