I have a for loop in which a function task is called. Each call to the function returns a string that is appended to an array. I would like to parallelise this for loop. I tried using & but it does not seem to work.
Here is the code not parallelised.
task (){ sleep 1;echo "hello $1"; }
arr=()
for i in {1..3}; do
arr+=("$(task $i)")
done
for i in "${arr[@]}"; do
echo "$i x";
done
The output is:
hello 1 x
hello 2 x
hello 3 x
Great! But now, when I try to parallelise it with
[...]
for i in {1..3}; do
arr+=("$(task $i)")&
done
wait
[...]
the output is empty.