When $() or `` is used bash can't overwrite it.
Here is the code:
#!/usr/bin/env bash
multiply() {
it=0
for i in ${arr[*]}; do
arr[$it]=$((i * 2)) # change the global array
it=$[ $it + 1 ]
done
echo ${arr[*]}
}
arr=(1 2 3 4 5 6 7 8 9 10)
new_arr=($(multiply))
echo "Before: " ${arr[*]}
echo "After: " ${new_arr[*]}
output:
Before: 1 2 3 4 5 6 7 8 9 10
After: 2 4 6 8 10 12 14 16 18 20
Why it works like that? Aren't the just supposed to assign output to array/variable?