Hello, Normally 'wait -n' will return the exit code of background process when they terminate before wait is runned. However, when bg processes and 'wait -n' runs inside a command substitution, bash loses control of bg process as soon as they exit.
nr=${1:-5} subjobs() { local -a pids for a in $(seq $nr); do ( sleep 0.$a exit $a ) & pids+=($!) done sleep $1 for a in $(seq $nr); do wait -n echo Got one child $? >&2 done } # run wait -n before jobs have fininshed echo Command substs no sleep a=$(subjobs 0 ) echo Command substs pipe no sleep b=$(subjobs 0 | cat) echo direct no sleep subjobs 0 # run wait -n after jobs have fininshed echo Command substs sleep a=$(subjobs 1) echo Command substs pipe sleep b=$(subjobs 1 | cat) echo direct sleep subjobs 1 Output: Command substs no sleep Got one child 1 Got one child 2 Got one child 3 Got one child 4 Got one child 5 Command substs pipe no sleep Got one child 1 Got one child 2 Got one child 3 Got one child 4 Got one child 5 direct no sleep Got one child 1 Got one child 2 Got one child 3 Got one child 4 Got one child 5 Command substs sleep Got one child 5 Got one child 127 Got one child 127 Got one child 127 Got one child 127 Command substs pipe sleep Got one child 127 Got one child 127 Got one child 127 Got one child 127 Got one child 127 direct sleep Got one child 1 Got one child 2 Got one child 3 Got one child 4 Got one child 5 And two related features requests: 1) It seems that pids args are ignored by wait when '-n' is specified. However, it would be a nice add_on to use the list of pids as a filter. 2) 'wait -n' lacks a way to get dead child PID, requiring some race conditions techniques to get it. Regards, -- Luiz Angelo Daros de Luca luizl...@gmail.com