Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

20
  • 9
    The line with wait in it basically lets all processes run, until it hits the nth process, then waits for all of the others to finish running, is that right? Commented Nov 26, 2015 at 23:03
  • 3
    @naught101 Yes. wait w/ no arg waits for all children. That makes it a little wasteful. The pipe-based-semaphore approach gives you more fluent concurrency (I've been using that in a custom shell based build system along with -nt/-ot checks successfully for a while now) Commented Mar 10, 2018 at 20:02
  • 3
    Note, if you have set -e the fourth solution wouldn't work for you, you'd need to change it to ((++i==1)) Commented Aug 20, 2020 at 12:14
  • 3
    For "N processes with a FIFO-based semaphore" remember to add a "wait" after the for/done loop to prevent the script going further while the last task is being executed Commented Dec 13, 2020 at 23:21
  • 5
    An even more elegant solution is suggested at unix.stackexchange.com/a/436713/192211 which is to use wait -n to get away with pure bash without batching. Commented Aug 6, 2021 at 20:45