I am trying to write a bash script that collects numerical results returned by several commands, and then does some simple arithmetic on the results. It would be great if the commands (which are independent) could run in parallel. The following works fine without the “&”, and takes 10 seconds. With the “&”, it takes 5 seconds as expected, but then gives the error message below. Grateful for any suggestions.
#!/bin/bash
set -eu
echo "Running .."
aa=$(sleep 5; ls |wc -l) & ###try with and without <&>
bb=$(sleep 5; ls |wc -l) & ###try with and without <&>
wait
cc=$(($aa + $bb))
echo $cc
run.sh: line 7: aa: unbound variable