简体   繁体   中英

What does symbol ^ mean in Batch script?

In this command:

FOR /F %%A IN ('TYPE "%InFile%"^|find /v /c ""')DO SET "Till=%%A"

what does the ^ mean?

The ^ symbol (also called caret or circumflex) is an escape character in Batch script. When it is used, the next character is interpreted as an ordinary character.

In your script, the output of the TYPE command to be written as the input to the FIND command.
If you don't use the escape character ^ before the | , then the regular meaning of | is a pipe character .

The Documentation says:

To display a pipe (|) or redirection character (< or >) when you are using echo, use a caret character immediately before the pipe or redirection character (for example, ^>, ^<, or ^| ). If you need to use the caret character (^), type two (^^).

The caret '^' character serves two purposes in Windows batch files:

1. line continuations:

~~~

@echo off

dir ^
/ad ^
c:\temp

~~~~

results in dir /ad c:\\temp, which lists only the directories in C:\\temp.

2. Escaping reserved shell characters & | ( < > ^.

Use a preceding caret to escape and print the character:

echo this pipe will print ^| but this one won't |
echo and this will print one caret ^^

Infinite Recursion describes the general behavior of ^ , but is a bit fuzzy on why it must be used within IN ('yourCommand') .

yourCommand is actually executed implicitly within its own CMD.EXE process using C:\\Windows\\system32\\cmd.exe /c yourCommand . Obviously the pipe must be included in the command in your case. But the entire line must be parsed by the batch parser before it can pass the IN() clause on to be executed. Without the ^ , the | confuses the batch parser. The ^ escapes (protects) the pipe during the initial batch parsing.

In the given example the caret is used for escaping the special symbol. Though it has other usages in the windows commands

1] In set it is a XOR operator:

set /a "_num=5^3"    &::0101 XOR 0011 = 0110 (decimal 6)

2] In findstr is used for regular expressions of finding string at the beginning of a line:

Echo 12G6 |FindStr /R "[^0-9]"  &::this will check for non-numeric characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM