In DOS scripting or commands, certain characters such as \ and / need to be displayed as part of a literal string. These special characters have meanings that differ from normal literal characters. Therefore, to include them in a string, you need to escape them.

In DOS, the ^ character is used as an escape character to indicate that the following character should be treated as a literal character.

For example, consider the following strings that contain special characters:

echo <p>
echo hello!
echo first > second
echo first & second
echo first< second
echo first | second

In the above example, <, >, &, |, and ! are special characters with specific meanings. Using them directly with echo will not yield the raw string.

General Escape Character (caret)

The ^ character is used to escape characters in Batch and command line scripts.

echo ^<p^>
echo hello^!
echo first ^> second
echo first ^& second
echo first ^< second
echo first ^| second

for example For example, echo first ^< second will print first < second. This escaping mechanism also works for HTML strings.

Incase if you enable double

  • Escaping Ampersand in Batch Script

The & character is special and has a unique meaning to separate commands. For instance, echo first & second would be treated as two separate commands.

E:\>echo first &second
first
'second' is not recognized as an internal or external command,
operable program or batch file.

To avoid that use escape character & used with ^ character in batch or command line script.

echo first ^&second

Alternatively, you can use double-quoted strings without the caret

echo "first &second"
  • Escaping Percentage (%) Character

Sometimes, the % character needs to be treated as a literal character. In such cases, it must be escaped with a caret.

echo 1110%

After escape characters, The line is

echo 111^%
  • Escaping Directory Paths Containing Spaces

When dealing with folder names containing spaces, you must escape the space to treat it as part of the literal string.

dir my dir

space escaped using caret

dir my^ dir
  • Escaping Redirection Operators

Redirection operators < and > have special meanings. To include them as part of the literal string, you need to escape them.

echo first ^< second ^> third

which outputs first < second > third

  • Escaping Piping Operators (|)

The piping operator | has a special meaning, indicating that the output of one command serves as input to another command.

echo first | second

which outputs first as input to second

to display as a literal string

echo first ^| second

Batch other escape characters

Some more characters(Asterisk, Question mark, Exclamatory mark, Backslash) characters escaped wih lcaret to display as a literal string.


echo first ^* REM Escaping Asterisk
echo first ^? REM Escaping Question Mark
echo first ^! REM Escaping Exclamatory Mark
echo first ^\ REM Escaping Backslash Character