Escaping Special Characters

Maintained on

Have you ever typed something like echo A & B, only to see A printed and an error saying B is not recognized as a command?

This happens because & is a special character that Command Prompt interprets as a command separator.

The mechanism that lets you treat such special characters as plain characters is called escaping.

This article explains how to escape special characters in Command Prompt and batch files.

What Are Special Characters?

The following are the main characters that Command Prompt interprets as having a special meaning.

CharacterOriginal meaning
&Run multiple commands in sequence
|Pass the output of the left command to the right one
<Read input from a file
>Write output to a file
( )Group commands into a block
^Escape character (treats the next character literally)
%Expand variables and batch file arguments
!Expand variables when delayed expansion is enabled

When you want to use these characters as ordinary text, you need to escape them.

Escaping with the Caret (^)

The escape character in Command Prompt is the caret (^).

Placing ^ immediately before a special character removes its special meaning and treats it as an ordinary character.

^special-character

Examples

This example displays a string containing & with echo.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo A ^& B
A & B
C:\users\user>

> and | work the same way. Add ^ to display them.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo 1 ^> 0 ^| 0 ^< 1
1 > 0 | 0 < 1
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

Without ^, everything after > is treated as a file name, and the output goes to a file instead of the screen.

Do not forget to escape

If you run echo 1 > 0 without ^, a file named 0 is created. Check that no unintended files were created.

Displaying ^ itself

^ is itself the escape character, so to display it, write it twice as ^^.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo 2^^3
2^3
C:\users\user>

Escaping % (%%)

% cannot be escaped with ^.

To use % as a literal character in a batch file, write it twice as %%.

@echo off
echo Progress: 50%%
echo Remainder: 7 %% 3

When you run this, it displays Progress: 50% and Remainder: 7 % 3.

The same applies when you calculate a remainder with set /a: write %% inside a batch file.

@echo off
set /a REMAINDER=7 %% 3
echo %REMAINDER%

Running this batch file displays 1.

When typing directly into Command Prompt

You only need to double % inside batch files. When typing directly into Command Prompt, write a single %, as in echo 50%. Similarly, the loop variable of the for command is written %%a in a batch file and %a when typed directly.

Enclosing in Double Quotes

&, |, <, >, (, and ) lose their special meaning when enclosed in double quotes (").

"string containing special characters"

Examples

This example handles a file whose name contains &. You do not need ^; enclosing the whole name in " is enough.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>type "Tom & Jerry.txt"
The contents of the file are displayed
C:\users\user>

Note that if you pass a double-quoted string to echo, the " characters are displayed as well.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>echo "A & B"
”A & B”
C:\users\user>
Inside double quotes, ^ and % behave differently

Inside double quotes, ^ is not an escape character but an ordinary character. Writing "A ^& B" therefore displays the ^ as well. On the other hand, % variable expansion still happens inside double quotes, so in a batch file you must still write %% there. Also, when delayed expansion is enabled, ! is expanded inside double quotes too.

Assigning Values with Special Characters Using set

When assigning a value containing special characters to a variable with set, the safe approach is to enclose the whole "name=value" in double quotes.

Without them, everything after & is run as a separate command.

@echo off
rem Bad: everything after & runs as a separate command
set TITLE=Tom & Jerry

rem Good: enclose the whole assignment in "
set "TITLE=Tom & Jerry"
echo "%TITLE%"

With the good example, Tom & Jerry is correctly assigned to the variable TITLE.

チェック

If you expand the variable without quotes, as in echo %TITLE%, the & in the expanded echo Tom & Jerry is interpreted as a separator again. Enclose it in ", as in echo "%TITLE%" (the " characters are displayed too).

This style also prevents unintended trailing spaces from becoming part of the value.

Escaping ) Inside a Block

If you display a string containing ) inside the ( ) of an if or for, the ) is interpreted as the end of the block.

@echo off
if exist "C:\Windows" (
    echo Found ^(Windows folder^)
)

As shown above, escape a ) that you use as a literal character inside a block as ^).

Take particular care when expanding an environment variable such as %ProgramFiles(x86)%. Because %...% is expanded before the block is parsed, the ) in the expanded C:\Program Files (x86) closes the block, so the first example below causes an error. You cannot add ^ to the result of a variable expansion, so enclose it in " as in the second example. Delayed expansion (!ProgramFiles(x86)!) also works.

@echo off
if 1==1 (
    echo %ProgramFiles(x86)%
)

if 1==1 (
    echo "%ProgramFiles(x86)%"
)

Note that ( does not cause problems in the middle of a block, so only ) needs escaping.

Continuing a Long Command with a Trailing ^

A ^ at the very end of a line makes the next line a continuation of that line.

This is useful for wrapping long commands to keep them readable.

@echo off
robocopy C:\Source C:\Backup ^
    /E ^
    /R:3 ^
    /W:5
チェック

If there is a space after the ^, you escape the space instead of the line break, and the continuation does not work. Always make ^ the very last character on the line.

Escaping Inside for /f and cmd /c

Inside the in ('...') of for /f, or in a command passed to cmd /c, the command is reinterpreted by another cmd.

Therefore, escape pipes and redirections with ^ so they are passed to the inner command. Note that when you enclose the whole command in ", as in cmd /c "tasklist | find /i notepad", ^ is not needed.

@echo off
for /f "tokens=*" %%a in ('tasklist ^| find "notepad.exe"') do (
    echo %%a
)

Writing ^| makes the | act as the pipe between tasklist and find, rather than being interpreted by the outer for command.

Summary of Escaping Methods

Character to displayNormal syntaxInside double quotes
& | < >^& ^| ^< ^>Write as is
( )^( ^)Write as is
^^^Write as is
% (batch file)%%%%
! (delayed expansion enabled)^^!^!
"^" (outside quotes only) (*)Cannot be escaped

* Outside double quotes, " can be treated literally with ^", but inside double quotes ^ is an ordinary character, so it cannot be escaped. To pass a " to a program, many programs expect \", but cmd itself ignores \ and treats the " as opening or closing a quoted section, so a following & and similar characters may still be executed as special characters.

Practice Questions

Question 1

Which command correctly displays A & B?

回答がサーバーに送信されることはありません
Question 2

How do you write % to display it as a literal character inside a batch file?

回答がサーバーに送信されることはありません
Question 3

Which command is the most appropriate way to safely assign Tom & Jerry to the variable NAME?

回答がサーバーに送信されることはありません

Summary

  • Special characters (& | < > ( )) can be escaped by placing ^ immediately before them
  • Write ^ itself as ^^, and % inside a batch file as %%
  • Enclosing text in double quotes lets &, |, and similar characters be used as is (but ^ becomes literal and % is still expanded)
  • When assigning a value with special characters using set, use the form set "name=value"
  • A trailing ^ lets you split a long command across multiple lines
#Command Prompt #Batch File #Escape #Special Characters #Caret