How to copy stdin into two files? How to copy stdin into two files? bash bash

How to copy stdin into two files?


You need tee.

$ echo 'foo' | tee f1.txt f2.txt 

or

$ echo 'foo' | tee f1.txt > f2.txt

to suppress the additional output to stdout.

I'm guessing your real problem could be how to read from input inside a script. In this case, refer to this question. That will give you something like

while read input; do    echo $input | tee -a f1.txt > f2.txtdone


Your script can look like this:

 #!/bin/bash read msg echo $msg > f1.txt echo $msg > f2.txt exit 0