I want to get the pids of two background processes,
sleep 20 & pid1=$\\!; sleep 10 & pid2=$\\!; echo \"pid1: $pid1, pid2: $pid2\"
and
I have accomplished this in the past by using bash arrays to hold the PIDs. I had a sequence of database imports to run and when handled sequentially they took ~8 hours to complete. I launched them all as background processes and tracked the list of PIDs to watch for completion and it got the processing time down to 45 minutes.
Here is an example of launching background processes, storing the PIDs in an array, and then printing all of the array values:
$ pids=()
$ sleep 20 &
22991
$ pids+=($!)
$ sleep 20 &
23298
$ pids+=($!)
$ j=0;for i in "${pids[@]}";do ((j=j+1));echo 'pid'$j': '$i;done
pid1: 22991
pid2: 23298