Programming Field

‘^’ (Using special chars (escaping)) - DOS/Command Prompt Reference

[Windows NT series] Used to treat following special character as a normal character. In addition, used to split a command line into multiple lines.


Usage of ‘^’

The caret symbol (a.k.a. circumflex accent) ‘^’ is used to escape reserved characters such as a double quotation mark ‘"’, a pipe operator (‘|’), and redirection symbols (‘<’ or etc.). For example,

echo A -^> B

will print ‘A -> B’ to the screen. If you write without ‘^’ as following:

echo A -> B

‘>’ will be treated as a redirection operator ‘>’, and this command will write ‘A -’ (and a newline) to the file named ‘B’.

Usage of ‘^’ 2

When a command line ends with ‘^’ character, the command line is treated as continuing to the next line. For example, the command line:

echo Hello, ^

will print:

Hello,   world

On the Command Prompt, typing a newline immediately after ‘^’ will show ‘More?’, prompting for more command lines.

* If and only if immediately after ‘^’ is a newline, the newline is treated as nothing (while remaining the escape effect of ‘^’). This causes that when ‘^’ makes a newline nothing, the character at the beginning of the line immediately after the newline is forcely treated as a regular character (i.e. escaped). A newline immediately after ‘^’ is ignored, but in this case the effect of ‘^’ remains, so when writing double newlines after ‘^’, the second newline is not treated as a command-line terminator character (treated as ‘^<newline>’, causing the newline treated as a regular character). As a result, the (third) line immediately after double newlines is treated as the same line for the first line.

‘^’ inside ‘" "’ has no effect. Therefore, the command line:

echo "Test ^

is interpreted as Echo statement ending on the first line, resulting executing the second line ‘Test"’.

* This behavior and the behavior ‘treating a newline immediately after ^ as nothing’ will result that ‘^’ character in the second line of the first block in the following sample is treated as a regular character (‘"’ character in the head of the second line is treated as a regular character due to ‘^’ character) and the third line is an independent command line. To avoid this, insert spaces or etc. to the head of the second (and third) line like the second block in the sample.

rem The following command line ends on the second line
execute "Strawberry" ^
"Mallow" ^

rem The following command line continues to the third line
execute "Strawberry" ^
  "Mallow" ^

To use ‘^’ as a regular character, write two characters consecutively like ‘^^’.

* The following command line ends on the second line (since ‘^’ in the head of the second line is treated as a regular character by ‘^’ character in the end of the first line). To avoid splitting, insert spaces or etc. to the head of the second to fourth lines.

hoge ^


Sample 1

foo.exe -f file1.txt ^
        -f file2.txt ^
        -f file3.txt ^
        -f file4.txt

Writes parameters in multiple lines by using ‘^’ to pass long parameter to ‘foo.exe’. In this case, spaces immediately before ‘^’ character and the first 8 spaces in the second to fourth lines are included as a part of the parameter.

Sample 2

set MYVAR=One^



By using the behavior that one newline immediately after ‘^’ is ignored and the next newline character is treated as a regular character, stores the value including newline characters to the environment variable with Set command. When executing this command, the value of the envrionment variable ‘MYVAR’ will be ‘One(newline)Two(newline)Three’.

* When treating a newline as a regular character by using ‘^’, its character code will be 10(0x0A; LF). (Even if a batch file is written with CR-LF newline characters, the CR character is not included to the newline string.)

Sample 3 (Batch file)

if not exist "E:\Foo\Bar\Piyo File.txt" (
    echo Error. ^(File not found.^)
    exit /b 1

Uses ‘^’ to treat parentheses as regular characters inside ‘( )’. (In this case, it is no problem to use ‘(’ (without ‘^’) instead of ‘^(’, but using ‘)’ (without ‘^’) instead of ‘^)’ causes unexpected behavior.)