TExcelTAD
Home › Windows CMD & Batch › Special Characters & Escaping

Special Characters & Escaping

A handful of characters have a special meaning on a CMD command line. To use them as plain text you must escape or quote them. The rules differ from Bash and PowerShell, and they even differ between the interactive prompt and batch files, which is why escaping is the trickiest corner of batch scripting.

Character Special meaning Literal form
& run the next command ^&
| pipe ^|
< > redirection ^< ^>
^ the escape character itself ^^
( ) blocks (only inside blocks/if/for) ^( ^)
% variables and arguments %% in a batch file
! delayed expansion (only when enabled) ^^! (or ^! inside quotes)
" quoting: turns most special characters off —

The caret ^

^ is CMD's escape character: the character right after it loses its special meaning, and the caret itself is removed.

cmd · try it yourself
@echo off
echo Tom ^& Jerry
echo 5 ^> 3 and 2 ^< 4
echo a ^| b
echo one caret: ^^
Tom & Jerry
5 > 3 and 2 < 4
a | b
one caret: ^

Without the carets, echo Tom & Jerry would print Tom and then try to run a program called Jerry; echo 5 > 3 would create a file named 3.

Line continuation

A ^ at the very end of a line escapes the line break: the command continues on the next line. It's useful for long commands:

cmd · try it yourself
@echo off
echo This is one long ^
command split over ^
three lines
This is one long command split over three lines

Make sure nothing (not even a space) follows the caret, or it escapes the space instead.

Quotes

Inside double quotes, &, |, <, > and ^ are ordinary characters. That's often simpler than escaping each one, but echo prints the quotes too:

cmd · try it yourself
@echo off
echo "Tom & Jerry > Itchy | Scratchy"
echo "a caret ^ stays as it is"
"Tom & Jerry > Itchy | Scratchy"
"a caret ^ stays as it is"

Quotes do not protect %: variables are still expanded inside them. The quote state also toggles at every ", so an odd number of quotes leaves the rest of the line "inside quotes".

Percent signs: %%

In a batch file a single % starts a variable or argument reference, and %% produces one literal %. A ^ does not escape % in a batch file.

cmd · try it yourself
@echo off
echo Progress: 50%%
echo Literal variable syntax: %%PATH%%
set /a "half=100 / 2"
echo %half%%% done
Progress: 50%
Literal variable syntax: %PATH%
50% done

At the interactive prompt it's different: %% is not special there, and a single % that doesn't form a known variable stays as it is. Code copied from the prompt into a batch file (or the other way round, such as for %f vs for %%f) breaks for exactly this reason.

Special characters in variables

Escapes are consumed when the line is parsed. Inside quotes in a set, special characters are stored safely. The difficulty is printing them, because %var% is expanded before CMD looks for &, |, < and >:

cmd · try it yourself
@echo off
set "pair=salt & pepper"
echo quoted: "%pair%"
setlocal enabledelayedexpansion
echo delayed: !pair!
endlocal
quoted: "salt & pepper"
delayed: salt & pepper

echo %pair% without quotes would split at the & and try to run pepper. Two safe ways to output such a value: inside quotes, or with delayed expansion: !var! is expanded after the special characters have been recognised, so its content is never re-parsed. That makes delayed expansion the most robust way to handle arbitrary text.

The alternative is to store the escape characters in the value, so they take effect when %var% is expanded:

cmd · try it yourself
@echo off
set "op=a ^> b"
echo %op%
set op=c ^^^& d
echo %op%
a > b
c & d

In the second set, without quotes, ^^^& is parsed once by set (giving ^& stored in the variable) and once more by echo (giving &). Every extra parsing pass needs another level of carets, which is why quoting and delayed expansion are usually the better choice.

Exclamation marks

When delayed expansion is enabled, ! is special too. A literal ! must be written ^! inside quotes and ^^! outside them (the extra caret survives the first parsing step):

cmd
C:\Users\ada>type bang.bat
@echo off
echo Hello there!
setlocal enabledelayedexpansion
echo Hello there!
echo Hello there^^!
echo "Hello there^!"
endlocal

C:\Users\ada>bang.bat
Hello there!
Hello there
Hello there!
"Hello there!"

The second line lost its !: a lone ! with no partner is simply removed. (This example is a plain session because the playground's cmd engine handles carets before ! differently from Windows.)

The simplest fix is not to enable delayed expansion around lines that print ! (see setlocal & Delayed Expansion).

Escaping inside for /f commands and blocks

Text inside for /f ('...') is parsed twice, once when the for line is read and once when the command runs, so pipes and redirections inside it need a caret:

cmd · try it yourself
@echo off
> words.txt echo pear
>> words.txt echo apple
>> words.txt echo fig
for /f %%w in ('type words.txt ^| sort') do echo %%w
apple
fig
pear

In a parenthesised block, a ) that is part of the text must be escaped as ^), otherwise it closes the block early:

cmd · try it yourself
@echo off
if 1==1 (
  echo price ^(incl. tax^): 12 EUR
  echo done
)
price (incl. tax): 12 EUR
done

Writing special characters to a file

A digit right before > at the end of echo text is a classic trap: echo retries 2>conf.txt doesn't write retries 2 into the file; it prints retries on the screen and redirects stream 2 (stderr) to the file. Putting the redirection first avoids that, and escaping handles the other special characters:

cmd · try it yourself
@echo off
> conf.txt echo level=2
>> conf.txt echo path=C:\app ^& more
>> conf.txt echo ^<tag^>
type conf.txt
level=2
path=C:\app & more
<tag>

A cheat sheet

You want to print In a batch file write
&, |, <, > ^& ^| ^< ^> or put it in quotes
^ ^^
% %%
! (delayed expansion on) ^^!
) inside a block ^)
an empty line echo. or echo(
a value with special characters "%var%" or !var!

Comparison with other shells

Shell Escape character Literal string
CMD ^ "..." (but % still expands)
Bash \ '...' (nothing expands)
PowerShell ` (backtick) '...' (nothing expands)

Learn more: Bash Quoting, PowerShell Strings, Redirection & nul.

Test yourself

How do you print Fish & Chips with echo in a batch file?

  1. echo Fish & Chips
  2. echo Fish ^& Chips
  3. echo Fish \& Chips
  4. echo Fish `& Chips

Which line prints 100% from a batch file?

  1. echo 100%
  2. echo 100^%
  3. echo 100%%
  4. echo "100%"

Why is !var! the safest way to print a value containing & or >?

  1. It adds quotes automatically
  2. It is expanded after special characters are parsed, so its content is not interpreted
  3. It escapes every character
  4. It only works for numbers