About
This page talks about shell redirection operator|operator in DOS.
The order of redirections is significant when redirecting a standard error stream to a standard output stream. See order of redirections
Handle
The standard predefined stream that can handle the operators are the below one and have a unique id
Handle | Numeric equivalent of handle (Unique id) | Description |
---|---|---|
nul or another string | nul | Suppress the character flow |
STDIN | 0 | Keyboard input |
STDOUT | 1 | Output to the Command Prompt window |
STDERR | 2 | Error output to the Command Prompt window |
UNDEFINED | 3-9 | These handles are defined individually by the application and are specific to each tool. |
Operators
Redirecting Output
> (Greater than)
Greater than > redirect:
- a standard output stream (stdout, stderr, …)
to:
- a standard output stream (generally stderr to stdout)
- or to new file
Syntax:
[SourceHandleId]>[&]TargetWord
where:
- SourceHandleId is the unique id of an handle by default 1 (stdout). One is the default handle (ie > is equivalent to >1 (stdout))
- & indicates that the targetWord is a file descriptor and not a filename
- TargetWord is a word that is subject to expansion (ie you can use a variable, …) but normally you will set it to
- TargetHandleId is the unique id of an handle if & is present
- or filePath a file path that must be created or overwritten.
Example:
- Redirect stderr to a new file
command 2> stderr.log
- Redirect stdout to a new file
command 1> stdout.log
Rem or simply
command > stdout.log
- Redirect stderr to a new file as variable
set FILE_STDERR=err.log
command 2> %FILE_STDERR%
- Redirecting the stderr to nothing (discarding it)
command 2>nul
- Redirecting standard output and error output to File.txt. The redirections of the stderr to stdout must appears after, the redirection to the file. See order_of_precedence
dir >c:\file.txt 2>&1
» (Append)
>> has the same syntax than > but it will append to the file. It will not create it or truncate as > does.
Redirecting Input - < (Less than)
<&: Reads the input from one handle and writes it to the output of another handle.
[StandardStreamHandleId]<[&]SourceWord
where:
- HandleId is a handle of a standard stream (generally 2)
- & indicates that the targetWord is a file descriptor and not a filename
- SourceWord is a word that is subject to expansion (ie you can use a variable, …) but normally you will set it to
- SourceHandleId is the unique id of an handle if & is present. Zero is the default handle (ie < is equivalent to <0 (stdin))
- or filePath a file path that must be created or overwritten.
Example:
- Sort the command input from a file (File.txt) The < operator opens the specified file name with read-only access.
sort <file.txt
REM or
<file.txt sort
- Redirecting handle 2 (STDERR) into handle 1 (STDOUT):
2<&1
Redirecting Output to Input - | (Pipeline or Command Chaining)
The | pipeline (See also DOS - Filter Commands) operator redirect the stdout (not the stderr) of the source program to the stdin of the target program
Syntax:
command parameter | command parameter
If you want to pipe :
- the stderr with the stdout, you need to redirect stderr to stdout.
mySourceCommand 2>&1 | myTargetCommand
- only stderr without stdout, you need to discard the stdout and redirect
mySourceCommand 2>&1 1>null | myTargetCommand
Order of precedence
The order of the standard streams redirection is important
The order of redirections is significant when you redirect the standard streams. The redirections of standard streams happens from left to right (and not from right to left)
dir >targetFile.txt 2>&1
redirects to the file:
- both standard output (file descriptor 1)
- and standard error (file descriptor 2)
dir 2>&1 >targetFile.txt
redirects only the standard output to the file.
the standard error was made a copy of the standard output before the standard output was redirected to the file.
The redirections may appears before or after the command
- Example 1
command 2>&1 >targetFile.txt
Rem is equivalent to
2>&1 >targetFile.txt command
- Example 2: A command can have both its standard input and standard output redirected.
sort < file_to_be_sorted.txt > sorted_file_list.txt
Rem is equivalent to
< fileToBeSorted.txt > fileSorted.txt sort
How to
Create a file with content
set myFile=myFile.sql
> %myFile% echo.WHENEVER SQLERROR EXIT SQL.SQLCODE
>> %myFile% echo.select * from dual;
>> %myFile% echo.exit;
>> %myFile% echo.
Content:
WHENEVER SQLERROR EXIT SQL.SQLCODE
select * from dual;
exit;