title: Bash script waiting for forked child processes to complete
body: <p>I have a script that iterates through JPG files in folder structure processing them.</p>
  
  <p>To speed things up I've forked the processing tasks like this:</p>
  
  <pre><code>#!/bin/sh
  SECONDS=0 #Start the clock!
  
  myloop () {
      local run=$1
      jpeg-transform "$file"
      stripexif "$file"
      ...other commands...
  }
  
  find /user/dan/testjpgs -name '*.jpg' -print0 | while read -d $'\0' file
      do myloop "$run" &amp; #fork myloop
      done    
  
  duration=$SECONDS
  echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."
  </code></pre>
  
  <p>The problem is that the duration calculation at the end of the script is happening immediately after the script is executed. It's not waiting for the child processes to complete and just returns 'Zero seconds elapsed'.</p>
  
  <p>I've tried inserting a "wait" command after the do...done loop but it doesn't have any effect.</p>