Every program started by cmd.exe has numbered input and output streams, the handles, and
the interpreter can connect them to files, devices and other programs. This page follows Microsoft’s
Using command redirection operators
and the cmd page.
Handles
| Handle | Name | Normally |
|---|---|---|
0 | STDIN | Keyboard input |
1 | STDOUT | Output to the console window |
2 | STDERR | Error output to the console window |
3 – 9 | undefined | Defined by each program |
A digit written directly before a redirection operator names the handle. Without one, <
means handle 0 and > means handle 1.
Operators
| Operator | Effect |
|---|---|
> file | Write the output to the file, creating it or replacing it. The file is opened write-only |
>> file | Append the output to the end of the file, creating it if needed |
< file | Read the input from the file, which must exist. Opened read-only |
n> file, n>> file, n< file | The same for handle n: 2>errors.txt |
>&n | Make the output handle a duplicate of handle n: 2>&1 sends errors where output goes |
<&n | Make the input handle a duplicate of handle n |
a | b | Pipe: the output of a becomes the input of b |
- A duplicated handle has the characteristics of the original: write-only stays write-only, and a handle with read-only access cannot be duplicated into a write-only one (Microsoft).
- The operators may stand anywhere in a command, including before it:
>out.txt echo hi, and the file name may be quoted. A space between the operator and the file name is allowed. - A space before the operator becomes part of the output of
echo:echo a >f.txtwritesawith a trailing space. Writeecho a>f.txt, or put the redirection first. A digit before>is a handle, soecho 5>f.txtwrites nothing; write(echo 5)>f.txtor>f.txt echo 5. - A block can be redirected as a whole:
(echo a & echo b) > out.txt. - The order of duplication matters.
cmd >out.txt 2>&1sends both streams to the file;cmd 2>&1 >out.txtsends errors to the console and output to the file, because handle 2 is copied from handle 1 before handle 1 is redirected. - The operator
<<(here-document) does not exist incmd.exe. - Microsoft: do not use pipes or redirection with
call.
Devices
| Name | Device |
|---|---|
nul | Discards output; reads as an empty file. >nul 2>&1 hides everything |
con | The console: >con writes to the screen even when output is redirected, and <con reads the keyboard |
prn, lpt1 – lpt9 | The printer and parallel ports |
aux, com1 – com9 | Serial ports |
clock$ | The DOS clock device, in DOS only |
These names are reserved in every folder in old DOS and Windows, even with an extension: a file called
nul.txt cannot be made with ordinary tools.
Pipes
In a | b both commands start at once and b waits for the output of
a (Microsoft). The filters find, findstr, sort and
more are made for this: dir /b | find "LOG" | more.
- Each side of a pipe runs in its own new
cmd.exe, started withcmd /c. A variable set on either side is lost, and a built-in command on either side does not change the batch file’s state. - Because each side is parsed again, a caret or
%that must reach the right command may need doubling, and the line is read once more for!. - The exit code of a pipeline is that of its last command.
>nulon the left side hides the output, so nothing flows to the right.
Combining commands
| Syntax | Meaning (Microsoft’s cmd page) |
|---|---|
a & b | Run a, then b |
a && b | Run b only if a succeeded (exit code 0, or no code) |
a || b | Run b only if a failed |
&& and || bind equally and are read from left to right (reported), so
a && b || c runs c if either a or b failed.
Success is the exit code of the command, so a command that fails without setting one counts as a
success; see Errorlevel and exit codes.
Tricks built on redirection
rem write a line without a trailing space, and an empty file
echo line one>log.txt
type nul > empty.txt
rem print text without a line end, and read a line of input from a pipe
<nul set /p "=Working... "
echo y| del /q *.tmp >nul 2>&1
rem send both streams of a program to a log, and to the screen only when it fails
tool.exe >> "%log%" 2>&1 || echo tool failed
rem read the first line of a file as input
set /p first=< "%file%"
Not described
Unknown
- Reference: the left-to-right rule for
&&with||, the pipe parsing and the old DOS device names are from reported analysis and the archived Microsoft page, not from current Microsoft documentation, and were not run oncmd.exefor this page.