Array arithmetic in bash Array arithmetic in bash shell shell

Array arithmetic in bash


There are several issues with your line of code:

P[0]= $(({arrKey[0,0]} * {arrT[0]} ))+ $(({arrKey[0,1]} * {arrT[1]})) ))
  • There is an additional space after the =, erase it.

    P[0]=$(({arrKey[0,0]} * {arrT[0]} ))+ $(({arrKey[0,1]} * {arrT[1]})) ))
  • It is incorrect to add two elements outside of an Arithmetic expansion.
    Remove the additional parentheses:

    P[0]=$(({arrKey[0,0]} * {arrT[0]} + {arrKey[0,1]} * {arrT[1]}))
  • either use a $ or remove the {…} from variables inside a $(( … )):

    P[0]=$(( arrKey[0,0] * arrT[0] + arrKey[0,1] * arrT[1] ))
  • Even if not strictly required, it is a good idea to quote your expansions:

    P[0]="$(( arrKey[0,0] * arrT[0] + arrKey[0,1] * arrT[1] ))"

Also, make sure that the arrKey has been declared as an associative array:

declare -A arrKey

To make sure the intended double index 0,0 works.