I was wondering about differences and relation between redirection and pipeline.
- Is pipeline only used to connect
stdout output of a command to stdin
input of another command? - Is redirection only used to connect
a stdout output of a command to a
file, and to connect a file to a
stdin input of a command? But there
seems to becommand >& 2
. - A pipeline
com1 | com2
can be
replaced by redirection ascom1 >
. Can the
tempfile; com2 < tempfile
replacement be modified to be
without using a file? - Can a redirection be replaced by a
pipeline?
Thanks and regards!
Best Answer
Yes. More precisely, in the shell, a pipe connects the standard output of the command on the left to the standard input of the command on the right. Even more precisely, what
foo | bar
means to the shell is:foo
in this child process.bar
in this child process.Yes. Ordinary redirections indicate a file by name:
>foo
. Another form of redirection indicates a file by descriptor. For example,>&2
means “redirect standard output to whatever file descriptor 2 is currently connected to”.On that topic, note that redirections are processed from left to right. For example, to redirect both standard output and standard error to the same file, use
foo >filename 2>&1
. The commandfoo 2>&1 >filename
typed in a terminal would first connect standard error to the terminal (which both standard output and standard error are still connected to at that point, so it wouldn't make any difference), then connect standard output to the file.Yes, but. The pipe construct in the shell creates an anonymous pipe; there are also named pipes.
Named pipes are a lot rarer than pipelines. They're used when it's necessary to connect two processes that are started independently; this doesn't happen very often, but it's nice to have it when necessary.
Note that the replacement you gave:
foo >tempfile; bar <tempfile
is different: firstfoo
writes all of its output to the temporary file, thenbar
starts running. With a pipeline, the commands run in parallel.Yes, but it's not very useful.