this is the generic script you're looking for. The only downside is your commands are in quotes which means syntax highlighting via your IDE will not really work. Otherwise, I have tried a couple of the other answers and this is the best one. This answer incorporates the idea of using wait <pid> given by @Michael but goes a step further by using the trap command which seems to work best.
#!/usr/bin/env bash
set -m # allow for job control
EXIT_CODE=0; # exit code of overall script
function foohandleJobs() {
for job in `jobs -p`; do
echo "CHLD"PID exit=> code${job}"
is CODE=0;
wait ${job} || CODE=$?"
if [[ $?"${CODE}" >!= 0"0" ]]; then
echo "at"At least one test failed";failed with exit code => ${CODE}" ;
EXIT_CODE=1;
fi
done
}
trap 'foo''handleJobs' CHLD
# trap command is the key part
DIRN=$(dirname "$0");
commands=(
"echo"{ 'x';echo 'a'; exit 1;"1; }"
"echo"{ 'y';echo 'b'; exit 0;"0; }"
"echo"{ 'z';echo 'c'; exit 2;"2; }"
)
clen=`expr "${#commands[@]}" - 1` # get length of commands - 1
for i in `seq 0 "$clen"`; do
(echo "${commands[$i]}" | bash) & # run the command via bash in subshell
echo "$i ith command has been issued as a background job"
done
wait; # wait for all subshells to finish
wait;
echo "EXIT_CODE => $EXIT_CODE"
exit "$EXIT_CODE"
# end
thanks to @michael homer for getting me on the right track, but using trap command is the best approach AFAICT.