Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I was wondering about differences and relation between redirection and pipeline.

  1. Is pipeline only used to connect stdout output of a command to stdin input of another command?
  2. 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 be command >& 2.
  3. A pipeline com1 | com2 can be replaced by redirection as com1 > tempfile; com2 < tempfile. Can the replacement be modified to be without using a file?
  4. Can a redirection be replaced by a pipeline?

Thanks and regards!

share|improve this question
    
It's generally a bad idea to post many questions in one. Here, the questions are strongly related, so it works, but usually it's better to post them separately, with a bit more context to it (e.g. “I ran this command line and that command line and I don't understand why they have different results”). Given your focus on learning, I suggest a book and experimentation (ask here when you don't understand something in the book or the result of your experiment, or want to know more). By the way, please don't use “tag lines” like “Thanks and regards”, they're implied (thank by voting/accepting). –  Gilles Jul 10 '11 at 0:12
add comment

2 Answers

up vote 6 down vote accepted
  1. 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:

    1. Create a pipe.
    2. Fork a child process, and connect the write end of the pipe to its standard output. Then execute foo in this child process.
    3. Fork a child process, and connect the read end of the pipe to its standard input. Then execute bar in this child process.
    4. Wait for both child processes to exit.
  2. 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 command foo 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.

  3. Yes, but. The pipe construct in the shell creates an anonymous pipe; there are also named pipes.

    mkfifo f
    cat f
    # (in another terminal)
    echo hello >f
    

    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: first foo writes all of its output to the temporary file, then bar starts running. With a pipeline, the commands run in parallel.

  4. Yes, but it's not very useful.

    cat input_file | some_command   # a uselessly complicated way of writing some_command <input_file
    echo hello | tee output_file    # a uselessly complicated way of writing echo hello >output_file
    
share|improve this answer
    
I am a little confused with the paragraph which starts with 'On that topic, note that redirections are processed from left to right......' on the 2. section. I found this statement conflicts with your later explanation. To apply this principle to the foo >filename 2>&1, does it mean, it first redirect the stdout to filename and then redirect stderr to whatever file descriptr stdout is connectted to? –  yangchenyun Aug 26 '12 at 12:45
    
@yangchenyun foo >filename 2>&1 means first redirect stdout to filename, then redirect stderr (= file descriptor 2) to the file that stdout (= file descriptor 1) is connected to. –  Gilles Aug 26 '12 at 14:43
    
I see, it is different from foo 2>&1 >filename. –  yangchenyun Aug 26 '12 at 17:31
add comment

1) Yes (beside stderr)

prg 2>&1 | workwithboth

2) Yes (beside stderr).

3) No. Not exactly.

com1 > tempfile; com2 < tempfile. 

will start com2 after finishing com1, while

com1 | com2 

might process the beginning of the output of com1 with com2 immediately. And yes - without a file: Use a pipeline.

4) You can use a window as a door, and a door as a window, but why should you? There were reasons to build doors and windows - they didn't fall from the sky.

share|improve this answer
    
Thanks! 1) what do you mean by the example? I was talking about pipeline only. 2) what do you mean by "besides stderr"? 3) Do you mean without using a file, pipeline can be replaced by redirection? 4) My question is for me to understand pipeline and redirection, not for real applications. –  Tim Jul 9 '11 at 23:54
    
1) You can pipeline the errorstream too. 2) redirection works for the errorstream too (&2) 3) No, I said the difference is, that the first cmd1 isn't finished, when cmd2 is started, opposed to using a file. 4) The example is to understand that doors and windows have different strengths, but if you insist, you can use them differently. –  user unknown Jul 10 '11 at 15:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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