title: Redirect output from multiple child processes in bash script
body: <p>I'm developing this script:</p>
  
  
  
  <pre class="lang-bash prettyprint-override"><code>#!/bin/bash
  
  # If no command line arguments, display a tiny usage message.
  if [[ $# -eq 0 ]]; then
    echo "Usage: $0 files..." &gt;&amp;2
    exit 1
  fi
  
  # Destroy child processes, when exiting.
  cleanup() {
    kill `jobs -p` 2&gt;/dev/null
  }
  
  # Call cleanup function on SIGINT, ie. Ctrl-C.
  trap "cleanup; exit 0" INT TERM EXIT
  
  # Start child processes continously outputting from the files given as command
  # line arguments. The filename is prepended on the line.
  for f in $*; do
    tail -f "$f" | awk '{ print "'"$f"':"$0 }' &amp;
  done
  
  # Wait for child processes to exit. Just to be sure, kill them all afterwards.
  wait
  cleanup
  </code></pre>
  
  <p>I use it like this:</p>
  
  <pre class="lang-bash prettyprint-override"><code>tailfiles *.log
  </code></pre>
  
  <p>What it does is interleaving the tail output from all the files, prepended with the filename (a bit like <code>grep -H</code> does). However, I can not pipe the output of the script. It simple gives me nothing:</p>
  
  <pre class="lang-bash prettyprint-override"><code>tailfiles *.log | grep error
  </code></pre>
  
  <p>Is it possible in bash to collect streams from multiple child processes into one output?</p>