Search code examples
unixshelltee

Unix and tee — chain of commands


In a Unix environment, I want to use tee on a chain of commands like so:

$ echo 1; echo 2 | tee file
1
2

$ cat file
2

Why does file only end up as having the output from the final command?

For the purposes of this discussion, let's assume I can't break them apart and run the commands separately.


Solution

  • Try:

     ( echo 1; echo 2 ) | tee file
    

    Without the parentheses, it's getting parsed as:

     echo 1 ; ( echo 2 | tee file )