TExcelTAD
Home › Terminal & Command Line › Redirection

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
text
             +-----------+ 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)

cmd · try it yourself
@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.txt
first 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>&1 sends errors wherever output is going, and the special device nul throws 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.txt stores hi with a trailing space, and echo Level 2> log.txt is 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.

powershell · try it yourself
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
  • $null plays the role of nul or /dev/null: 2>$null discards errors.
  • > is shorthand for Out-File; use Out-File -Encoding, Set-Content or Add-Content when 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 prefer Out-File -Encoding utf8 there.
  • There is no < input operator in PowerShell; read files with Get-Content file | command instead.

Linux & macOS (bash/zsh)

bash · try it yourself
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/null is the "black hole" file that swallows anything written to it.
  • cmd > file 2>&1 (and the shorthand cmd &> file) puts both streams in one file. The order matters: 2>&1 copies wherever stream 1 points at that moment, so cmd 2>&1 > file still shows errors on screen.
  • A here-document feeds several lines as input without a separate file:
bash · try it yourself
sort << 'EOF'
pear
apple
mango
EOF
apple
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.

zsh · try it yourself
print "saved twice" > a.txt > b.txt
cat a.txt b.txt
setopt noclobber
print "oops" > a.txt
print "forced" >| a.txt
cat a.txt
saved 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?

  1. >
  2. >>
  3. 2>
  4. <

What is the PowerShell equivalent of 2>/dev/null or 2>nul?

  1. 2>$null
  2. 2>nul:
  3. | Out-Null 2
  4. -Silent

In bash, why does ls a missing 2>&1 > out.txt still print the error on screen?

  1. bash ignores 2>&1
  2. Redirections apply left to right, so stderr was pointed at the terminal before stdout moved to the file
  3. ls writes errors to stdout
  4. out.txt is read-only