Unix and the triple command chain

In a Unix environment, I want to use teethe following commands in a chain:

$ echo 1; echo 2 | tee file
1
2

$ cat file
2

Why filedoes it end up with a way out of the last team?

For the purposes of this discussion, suppose I cannot break them apart and execute the commands separately.

+5
source share
2 answers

Try:

 ( echo 1; echo 2 ) | tee file

Without parentheses, it is treated as:

 echo 1 ; ( echo 2 | tee file )
+4
source

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

Just put them in parentheses:

(echo 1; echo 2) | tee file
+5
source

All Articles