TExcelTAD
Home › Linux & Bash › Pipes & Redirection

Pipes & Redirection

Every program started from Bash gets three standard streams: it reads from standard input and writes normal output to standard output and error messages to standard error. Redirection connects these streams to files, and pipes connect one program's output to the next program's input. This is the core idea of the Unix philosophy: small tools, combined.

Stream Number Default
standard input 0 the keyboard
standard output 1 the terminal
standard error 2 the terminal

Output redirection: > and >>

bash · try it yourself
echo "first run" > status.txt
echo "second run" > status.txt
cat status.txt
echo "line A" >> history.txt
echo "line B" >> history.txt
cat history.txt
second run
line A
line B

> creates or truncates the file; >> appends. set -o noclobber makes > refuse to overwrite existing files (>| forces it).

Errors go to a different stream

Error messages are written to stream 2, so > alone doesn't capture them:

bash · try it yourself
ls report.txt missing.txt > out.txt
echo "--- out.txt contains:"
cat out.txt
touch report.txt
ls report.txt missing.txt > out.txt 2> err.txt
echo "--- out.txt:"; cat out.txt
echo "--- err.txt:"; cat err.txt
ls: cannot access 'report.txt': No such file or directory
ls: cannot access 'missing.txt': No such file or directory
--- out.txt contains:
--- out.txt:
report.txt
--- err.txt:
ls: cannot access 'missing.txt': No such file or directory

The first time, report.txt didn't exist either, so both messages went to the screen and out.txt stayed empty.

Syntax Meaning
cmd > file stdout to file
cmd 2> file stderr to file
cmd > file 2>&1 both to the same file (order matters!)
cmd &> file both to the same file (Bash shorthand)
cmd 2> /dev/null discard errors
cmd > /dev/null 2>&1 discard everything

/dev/null is a special file that swallows everything written to it.

2>&1 and why order matters

2>&1 means "make stream 2 go wherever stream 1 goes right now". Redirections are processed left to right:

bash · try it yourself
touch present.txt
ls present.txt absent.txt > both.txt 2>&1
echo "--- both.txt:"; cat both.txt
ls present.txt absent.txt 2>&1 > only-out.txt
echo "--- only-out.txt:"; cat only-out.txt
--- both.txt:
ls: cannot access 'absent.txt': No such file or directory
present.txt
ls: cannot access 'absent.txt': No such file or directory
--- only-out.txt:
present.txt

In the second command, stream 2 was pointed at the terminal (where stream 1 was at that moment) before stream 1 moved to the file, so the error still appeared on screen.

Input redirection: <, <<<

< feeds a file to a command's standard input. <<< (a here-string) feeds a single string:

bash · try it yourself
printf 'charlie\nalpha\nbravo\n' > names.txt
sort < names.txt
wc -w <<< "three little words"
read -r first rest <<< "Ada Lovelace, mathematician"
echo "first=$first rest=$rest"
alpha
bravo
charlie
3
first=Ada rest=Lovelace, mathematician

Here-documents (<< EOF) feed several lines; they get their own lesson.

Pipes

cmd1 | cmd2 connects the standard output of cmd1 to the standard input of cmd2. Both run at the same time, and data flows through without temporary files:

bash · try it yourself
cat > access.log <<'LOG'
GET /home 200
GET /cart 500
POST /cart 200
GET /home 200
GET /admin 403
GET /home 200
LOG
grep -v ' 200
  

 access.log
echo "---"
cut -d' ' -f2 access.log | sort | uniq -c | sort -rn
echo "---"
awk '{ print $3 }' access.log | sort -u | paste -sd,
GET /cart 500
GET /admin 403
---
      3 /home
      2 /cart
      1 /admin
---
200,403,500

Each tool does one small thing: cut extracts, sort groups, uniq -c counts, sort -rn ranks. Building a pipeline step by step, checking the output after each stage, is the best way to write one.

Only standard output goes into a pipe. Use 2>&1 | (or |&) to send errors through as well:

bash · try it yourself
ls nothing-here | wc -l
ls nothing-here 2>&1 | wc -l
ls: cannot access 'nothing-here': No such file or directory
0
1

Command substitution

$(command) runs a command and inserts its output into another command line. It's how you put a command's result into a variable:

bash · try it yourself
printf 'a\nb\nc\n' > items.txt
count=$(wc -l < items.txt)
echo "There are $count items"
echo "Longest file name here: $(ls | awk '{ print length, $0 }' | sort -rn | head -n 1 | cut -d' ' -f2)"
There are 3 items
Longest file name here: items.txt

Trailing newlines of the output are removed. The old backtick syntax `command` does the same, but $( ) is easier to read and can be nested.

Test yourself

Which command saves both normal output and errors of make into build.log?

  1. make > build.log
  2. make 2> build.log
  3. make > build.log 2>&1
  4. make 2>&1 > build.log

What is /dev/null used for?

  1. Storing temporary files
  2. Discarding output written to it
  3. Reading null bytes
  4. Logging errors

What does | connect?

  1. stderr of the left command to stdin of the right
  2. stdout of the left command to stdin of the right
  3. Two files
  4. Two variables