title: Use bash wait in for-loop
body: <p>(I have searched and expected this question to have been asked before but couldn't find anything like this although there are plenty of similar questions)</p>
  
  <p>I want this for-loop to run in 3 different threads/processes and <code>wait</code> seem to be the right command</p>
  
  <pre><code>for file in 1.txt 2.txt 3.text 4.txt 5.txt
          do something lengthy &amp;
          i=$((i + 1))
          wait $!
  done
  </code></pre>
  
  <p>But this construct, I guess, just starts one thread and then wait until it is done before it starts the next thread. I could place <code>wait</code> outside the loop but how do I then</p>
  
  <ol>
  <li>Access the pids?</li>
  <li>Limit it to 3 threads?</li>
  </ol>