Home > Back-end >  Why `for x in {1..3}; do echo "$x" && sleep 2 ; done | tee output123` doesn't need `t
Why `for x in {1..3}; do echo "$x" && sleep 2 ; done | tee output123` doesn't need `t

Time:06-08

command:

for x in {1..3}; do echo "$x" && sleep 2 ; done | tee output123 writes

1
2
3

correctly to output123, why -a for tee is not necessary here?

And I know, for :

for x in {1..3}; do echo "$x" | tee -a output123 && sleep 2 ; done ,

it needs tee -a.

I guess there's something to do with the bash loop?

CodePudding user response:

In the first one the loop runs and all its output is given to a single tee. In the latter one tee is run for each loop iteration so without -a each execution of it will just overwrite the file.

Note that these aren’t equivalent if the file already exists.

  •  Tags:  
  • bash
  • Related