5

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.

1
  • As well as creating an explicit sub-shell, you can use { echo 1; echo 2; } | tee file to get the output into a file. Note that { has to be separated from the command by a space, and } must appear where a command could appear (so it is after the second semicolon). The other alternative is always to create a new script containing the two indivisible commands and piping the output of that to tee: conjoined-twin-processes | tee file. Jul 10, 2015 at 3:00

2 Answers 2

5

It has only the output of the second command, as the semicolon indicates a new statement to the shell.

Just put them into parentheses:

(echo 1; echo 2) | tee file
4

Try:

 ( echo 1; echo 2 ) | tee file

Without the parentheses, it's getting parsed as:

 echo 1 ; ( echo 2 | tee file )

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.