# Sum all Sub Array in Linux

Consider Array (1 2 3) , Below are the Sub-Array for this Array and their sum.
(1): Sum of this sub-array is 1
(2): Sum of this sub-array is 2
(3): Sum of this sub-array is 3
(1 2): Sum of this sub-array is 3
(2 3): Sum of this sub-array is 5
(1 2 3): Sum of this sub-array is 6
So, there are 6 Sub Array in this Particular Array.
Now, we need to find out sum of all these Array using shell scripting.
NOTE : You can remove the outer most while loop and just pass the value of k where k is the length of Sub Array in order to find Sum of Sub Array having length N(which is K here in our case).

`arr=(1 2 3) ## Array of 3 elementslen1=\${#arr[@]} ## Length of Array arrSumSubArr=()## created Empty ArrayLenOfSubArr=1 ## Minimum length of Sub Array is 1.while [ \$LenOfSubArr -le \$len1 ]dok=\$LenOfSubArrj=0i=0k1=\$(( \$k - 1))k2=\$kloopLen=\$((\$len1 - \$k2 + 1))while [ \$i -lt \$loopLen ] ## This will run for all Sub Array of Particular Length using Variable LenOfSubArr.dosum_arr=0while [ \$j -lt \$k ] ## This loop will sum values of all elements present in each Sub Arraydosum_arr=\$((\$sum_arr + \${arr[j]}))j=\$(( \$j + 1))doneSumSubArr+=\$sum_arri=\$(( \$i + 1))j=\$(( \$j - \$k1))k=\$(( \$k + 1))doneLenOfSubArr=\$(( \$LenOfSubArr +1))doneecho \${SumSubArr[*]}`