Why does the who > lp creates a file but who | lp leads the output to program lp?

What is the underlying principle behind these two simple commands? Can somebody explain it with an example?

  • A common mistake here is to think that lp file.txt and (for example) cat file.txt | lp are the same (that is, that the pipe somehow assemblies the file, row by row, and makes an argument of that data, and passes it to lp) - this is a common mistake, because the result (in this case, what is printed) may be identical. But those interfaces (argument(s), and a pipe to STDIN) are not at all the same. As an illustration, try it with echo, and not lp. – Emanuel Berg Nov 20 '12 at 22:43

> is redirection to a file. | is a redirection to a process through the use of a pipe.

The latter essentially points file descriptor 1 (STDOUT) of one process to a pipe which leads to file descriptor 0 (STDIN) of another process. A file redirection operation is not as complex, it merely takes an FD and dumps it to a file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.