Bash

Background Jobs + wait

admin by @admin ADMIN
1d ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Run several commands in parallel with `&`, then wait for all of them with `wait`. Collect exit codes via $! and check after wait returns.
Bash
Raw
# Fire 3 downloads in parallel
curl -sO https://example.com/a.zip &  pid1=$!
curl -sO https://example.com/b.zip &  pid2=$!
curl -sO https://example.com/c.zip &  pid3=$!

# Wait for all
wait "$pid1" "$pid2" "$pid3"

# Or wait + capture each exit code
declare -A status
for pid in "$pid1" "$pid2" "$pid3"; do
    wait "$pid"
    status[$pid]=$?
done

for pid in "${!status[@]}"; do
    echo "pid $pid exited ${status[$pid]}"
done
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.