Redirection
Every command you start gets a few streams: one to read input from and two to write to. By default all of them are connected to your terminal window: you type the input, and both normal output and error messages appear on screen. Redirection reconnects a stream to a file instead. It is how you save a command's output, feed a file into a command, or silence noisy errors.
| Stream | Number | Default | Used for |
|---|---|---|---|
| standard input (stdin) | 0 | keyboard | data a command reads |
| standard output (stdout) | 1 | terminal | normal results |
| standard error (stderr) | 2 | terminal | error and diagnostic messages |
+-----------+ 1 stdout --> terminal (or > file)
stdin 0 --> | command |
(or < file) +-----------+ 2 stderr --> terminal (or 2> file)The good news: the basic operators >, >>, 2> and < look the same in CMD, PowerShell, bash and zsh. The
differences are in the details, which this lesson covers shell by shell.
Windows (CMD)
@echo off
>log.txt echo first line
>>log.txt echo second line
type log.txt
sort < log.txt
(echo all good& 1>&2 echo something failed) > out.txt 2> err.txt
echo --- out.txt
type out.txt
echo --- err.txt
type err.txtfirst line
second line
first line
second line
--- out.txt
all good
--- err.txt
something failed
>creates or overwrites a file,>>appends to it,<feeds a file into a command's input.2>redirects errors,2>&1sends errors wherever output is going, and the special devicenulthrows output away:del temp.txt 2>nul.1>&2 echo ...writes a message to stderr, which is how batch scripts report errors.
Note: In CMD, writing the redirection first (
>log.txt echo hi) avoids two traps:echo hi > log.txtstoreshiwith a trailing space, andecho Level 2> log.txtis read as "redirect stream 2", so the digit disappears from the file.
Windows (PowerShell)
PowerShell has more than two output streams (success, error, warning, verbose, debug, information), and each has a
number you can redirect. *> redirects all of them at once.
function Invoke-Task {
Write-Output "task finished"
Write-Warning "disk almost full"
Write-Error "could not reach server"
}
Invoke-Task > out.txt 2> err.txt 3> warn.txt
"--- out.txt"; Get-Content out.txt
"--- warn.txt"; Get-Content warn.txt
"--- quiet run:"
Invoke-Task 2>$null 3>$null
"report" | Out-File report.txt
Add-Content report.txt "one more line"
Get-Content report.txt--- out.txt
task finished
--- warn.txt
disk almost full
--- quiet run:
task finished
report
one more line
$nullplays the role ofnulor/dev/null:2>$nulldiscards errors.>is shorthand forOut-File; useOut-File -Encoding,Set-ContentorAdd-Contentwhen you need control over the encoding or want to write without the formatting system.- PowerShell 7 writes UTF-8 (no BOM) by default. The older Windows PowerShell 5.1 writes UTF-16 with
>, which surprises tools that expect plain text, so preferOut-File -Encoding utf8there. - There is no
<input operator in PowerShell; read files withGet-Content file | commandinstead.
Linux & macOS (bash/zsh)
echo "first line" > log.txt
echo "second line" >> log.txt
cat log.txt
sort -r < log.txt
ls log.txt missing.txt > out.txt 2> err.txt
echo "--- out.txt:"; cat out.txt
echo "--- err.txt:"; cat err.txt
ls log.txt missing.txt > all.txt 2>&1
echo "--- all.txt:"; cat all.txt
ls missing.txt 2> /dev/null || echo "(error hidden)"first line
second line
second line
first line
--- out.txt:
log.txt
--- err.txt:
ls: cannot access 'missing.txt': No such file or directory
--- all.txt:
ls: cannot access 'missing.txt': No such file or directory
log.txt
(error hidden)
/dev/nullis the "black hole" file that swallows anything written to it.cmd > file 2>&1(and the shorthandcmd &> file) puts both streams in one file. The order matters:2>&1copies wherever stream 1 points at that moment, socmd 2>&1 > filestill shows errors on screen.- A here-document feeds several lines as input without a separate file:
sort << 'EOF'
pear
apple
mango
EOFapple
mango
pear
zsh differences
zsh accepts everything above, and adds multios: redirecting the same stream twice writes to both targets,
like a built-in tee. With setopt noclobber, > refuses to overwrite an existing file and >| forces it.
print "saved twice" > a.txt > b.txt
cat a.txt b.txt
setopt noclobber
print "oops" > a.txt
print "forced" >| a.txt
cat a.txtsaved twice
saved twice
/w/main.zsh:4: file exists: a.txt
forced
(bash has the same noclobber option: set -o noclobber.)
Comparison
| Task | CMD | PowerShell | bash / zsh |
|---|---|---|---|
| Output to file (overwrite) | cmd > f |
cmd > f |
cmd > f |
| Append | cmd >> f |
cmd >> f |
cmd >> f |
| Errors to file | cmd 2> f |
cmd 2> f |
cmd 2> f |
| Both to one file | cmd > f 2>&1 |
cmd *> f |
cmd > f 2>&1 or cmd &> f |
| Discard output | cmd > nul |
cmd > $null |
cmd > /dev/null |
| File as input | cmd < f |
Get-Content f | cmd |
cmd < f |
| Write to stderr | 1>&2 echo msg |
Write-Error msg |
echo msg >&2 |
Learn more: Bash Pipes & Redirection, Batch Redirection, PowerShell Output Streams.
Test yourself
Which redirection appends to a file instead of overwriting it?
- >
- >>
- 2>
- <
What is the PowerShell equivalent of 2>/dev/null or 2>nul?
- 2>$null
- 2>nul:
- | Out-Null 2
- -Silent
In bash, why does ls a missing 2>&1 > out.txt still print the error on screen?
- bash ignores 2>&1
- Redirections apply left to right, so stderr was pointed at the terminal before stdout moved to the file
- ls writes errors to stdout
- out.txt is read-only