Say we have a BASH array with integers:
declare -a arr=( 1 2 3 )
and I want to do an arithmetic operation on each element, e.g. add 1. Is there an altenative to a for loop:
for (( i=0 ; i<=${#arr[@]}-1 ; i++ )) ; do
arr[$i]=$(( ${arr[$i]} + 1 ))
done
I have tried a few options:
arr=$(( ${arr[@]} + 1 ))
fails, while
arr=$(( $arr + 1 ))
results in
echo ${arr[@]}
2 2 3
thus only the first (zeroth) element being changed.
I read about awk
solutions, but would like to know if BASH arithmetics support such batch operations on each array element.
arr[$i]=$(( ${arr[$i]} + 1 ))
toarr[$i]++
– Gallagerarr(:) = arr(:) + 1
orarr3(:) = arr1(:) * arr2(:)
, so I had the hope that BASH would be capable. Sorry, I cannot reproduce thearr[$i]++
thing. Care to explain? – Baboon(( $arr[$i]++ ))
– Gallager