4

I'm trying to add some number in an array.

Example:

array=( 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 )

I've done it this way but I want to use an array and then sum up the numbers in a shell script.

num1=2
num2=4
num3=8
num4=10
num5=12
num6=14
num7=16
num8=18
num9=20
sum=$((num1+num2+num3+num4+num5+num6+num7+num8+num9))
echo "The sum is: $sum"
  • Are you fussy about the language of the shell script? You've tagged the question with bash, but don't specify elsewhere. – Progrock Dec 4 '15 at 18:45
12

You can do:

$ array=( 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

$ echo "${array[@]/,/+}" | bc               
110
  • ${array[@]/,/+} is a parameter expansion pattern that replaces all , with + in all elements of the array

  • Then bc simply does the addition

Let's break it up a bit for clarification:

$ array=( 2, 4, 6, 8, 10, 12, 14, 16, 18, 20)

$ echo "${array[@]/,/+}"                     
2+ 4+ 6+ 8+ 10+ 12+ 14+ 16+ 18+ 20

$ echo "${array[@]/,/+}" | bc
110
  • Thanks! This looks to be what I'm looking for. I did not know about bc. I will definitely keep this for future use. – DSH72 Dec 4 '15 at 19:46
  • 4
    No need for commas: array=( 2 4 6 8 10 12 14 16 18 20); echo "${array[@]/%/+}0" | bc – Dennis Williamson Dec 4 '15 at 22:56
  • @DennisWilliamson OP has literal commas in the array declaration..i have used it verbatim :) – heemayl Dec 5 '15 at 0:45
  • 1
    I don't think the OP was being literal. All this does is lead to confusion about BASH array constructs. – Progrock Dec 5 '15 at 8:36
  • @Progrock Quoting OP I've done it this way but I want to use an array and then sum up the numbers ..I think OP is clear over there.. – heemayl Dec 5 '15 at 8:40
3

In Python it's very simple to add a list of numbers.

$ python -c 'print sum([1,2,3])'

Outputs 6.

  • not quite..the array elements contain literal ,s..perhaps you can do rstrip(',') on all elements and then do the addition..anyway i think OP is meaning shell here, not a high level language :) – heemayl Dec 4 '15 at 18:54
  • It works as is. – Progrock Dec 4 '15 at 19:16
  • It's also quite easy in Turbo Pascal. – Marcin Kaminski Dec 5 '15 at 1:06
3
num1=2
num2=4
num3=8
array=($num1 $num2 $num3)
declare -i sum
IFS=+ sum="${array[*]}"
echo $sum

Output:

14

See: help declare

  • 1
    No need for declare or IFS: array=(2 4 8); ((sum = ${array[@]/%/+}0)); echo "$sum" – Dennis Williamson Dec 4 '15 at 22:59
2

Using a loop in Bash:

#!/bin/bash

nums=(1 2 3)
total=0
for n in ${nums[@]}
do
  (( total += n ))
done
echo $total

Outputs 6.

  • (( total += n )) – Dennis Williamson Dec 4 '15 at 23:00
  • @DennisWilliamson, I think that is a little less intuitive for beginners, but it's most likely more idiomatic and cleaner. Thanks, I've modified my answer. – Progrock Dec 4 '15 at 23:15
1

One could also use awk to perform the looping summation:

$ echo "${arr[@]}"
1 2 3
$ awk 'BEGIN{for (arg in ARGV) sum += arg;print sum}' "${arr[@]}"
6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.