Search code examples
linuxbashtee

How do I "tee" variable number of times in a bash "for loop"?


I understand I can:

ssh archive_server -l user -n "cat text.csv"|tee -a text1.csv|tee -a text2.csv|tee....|tee -a text10.csv

Is there a way to do it a a loop?

for i in `seq 1 10`; do
  echo $i
  tee ???
done

Solution

  • You don't need a loop. tee can be given multiple filename arguments, so just give all the output files at once:

    cat text.csv | tee -a text{1..10}.csv
    

    If the number of files is dynamic, you can use a loop in $() to insert the filenames into the command line:

    cat text.csv | tee -a $(
        for i in $(seq 1 $filecount); do
            echo text$i;
        done)
    

    Just make sure that you don't have any whitespace in the output filename prefix, as the spaces will be treated as argument delimiters.