Added Two Arrays But With Errors

Please could someone have a look at the code below and spot the cause of the error.
Thanks in advance.
CODE BELOW: (code tags now added) My apologies please, that was my first time here.:slight_smile:

#! /bin/bash
# file: whileloop.sh

arr1=(2 4 6 8)
arr2=(3 6 9 12)
arr3=()

indextotal=(${#arr1[@]})
i=0
sum=0
while [[ ${#arr1[@]} -eq ${#arr2[@]} ]] && [[ $i -le $indextotal ]] # ${#arr[@]} depicts the size of an array
do
    sum=$(( "${arr1}"+"${arr2}" )) 
    #echo $sum
    arr3=("${arr3[@]}" "$sum")
    i=$(( i+1 ))
done
echo "${arr3[@]}"

OUTPUT BELOW

whileloop.sh: line 13: +  : syntax error: operand expected (error token is "+  ")
5 10 15 20

Thanks,
Chiadi

That happens in the last loop. You are running loop variable i one too high for the array elements (i = 4; with elements only from 0 to 3). Try

[[ $i -lt $indextotal ]]

in lieu of -le .

Run script with the xtrace ( -x ) option set to see what is going on.

1 Like

You nailed it.
Thanks a lot.
I am a newbie.

------ Post updated at 01:51 PM ------

Thanks to you RudiC for the extra suggestion of using xtrace in troubleshooting. I'm grateful. :slight_smile:
That was my first post here and I only joined yesterday.

Regards,
Chiadi

You're welcome.

Thanks for trying to edit code tags in; but - use code tags for code and data, not PHP tags. I removed those in post#1 for you.

Thanks a lot.