Unix and tee — chain of commands
Asked Answered
R

2

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.

Rivera answered 17/5, 2010 at 18:54 Comment(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.Stefanstefanac
A
4

Try:

 ( echo 1; echo 2 ) | tee file

Without the parentheses, it's getting parsed as:

 echo 1 ; ( echo 2 | tee file )
Alenealenson answered 17/5, 2010 at 18:58 Comment(0)
E
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
Enwind answered 17/5, 2010 at 18:56 Comment(0)
A
4

Try:

 ( echo 1; echo 2 ) | tee file

Without the parentheses, it's getting parsed as:

 echo 1 ; ( echo 2 | tee file )
Alenealenson answered 17/5, 2010 at 18:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.