difference between pipelining and redirection in linux
Can anyone tell me the difference? for exampl开发者_JAVA百科e:
if I have a filea.txt
with the following content:
a
b c
what would be the difference between cat a.txt | cat
and cat < a.txt
Piping works from one process to another (the cat
s in the first example), and hence requires two processes cooperating. Redirection is handled by the shell itself. This can matter when doing things in the shell such as working with variables.
The redirection does not "simulate STDIN". When you redirect, the file is the stdin for the process. In particular, many programs have different behavior if the input is a regular file than if it is a pipe or a tty, so you may get different behavior. For example:
$ < file perl -E 'say "is a regular file" if -f STDIN' is a regular file $ cat file | perl -E 'say "is a regular file" if -f STDIN'
Firstly, two results are same. Nothing to say.
For the work principle of cat a.txt | cat
, the first cat takes argument a.txt
, then prints its content. You pipe the stdout
of the first to stdin
of the second. The second cat
finds no argument so it reads content from stdin
, and prints it.
Because you use <
in the second command, system replaces stdin
of cat
with file stream of a.txt
. Anything else is same as the second cat in the first case.
精彩评论