Shell: pipes and redirections
In short: In the shell a pipe (|) connects the output of one program to the input of the next, and redirections (>, <) steer input and output into files. From these you build powerful command chains out of small tools.
In more detail: Every program has three standard channels: input (stdin, 0), output (stdout, 1) and error output (stderr, 2). The shell can rewire them.
In Depth
- Pipe:
cat log.txt | grep ERROR | sort | uniq -c | sort -rn | headfilters, counts and ranks error messages. The programs run concurrently. - Output to a file:
command > fileoverwrites,command >> fileappends. - Input from a file:
command < file. - Separating errors:
command 2> errors.txt, both togethercommand > all.txt 2>&1or short&> all.txt(bash). Mind the order:2>&1copies where 1 currently points. - Discarding:
command > /dev/null 2>&1. - Show and save at once:
command | tee file.txt. - Command substitution:
$(command)inserts one command’s output into another line. - Here-strings and here-docs:
<<< "text"and<<EOF ... EOFpass text as input. - Success in pipes: By default only the last command’s return value counts.
set -o pipefailreports earlier failures too.
See also: writing shell scripts, Bash