Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through...

Script that i am using is below :

#!/bin/bash
sum=0
for i in $@
do
    sum=$sum+$1
    echo $sum
    shift
done

I am executing the script as below

sh script 4 5 6 7

the output that I am getting is

0+4
0+4+5
0+4+5+6
0+4+5+6+7
#!/bin/bash
sum=0
for i in $@; do sum=$((sum+i)); done
echo $sum
exit 0
~/unix.com$ bash script 4 5 6 7
22

See man bash for more infos

1 Like

may be this ?

$ cat script.sh
#!/bin/bash
sum=0
for i in $@
do
    sum=`expr $sum + $i`
    echo $sum
    shift
done

output:

$ ./script.sh 4 5 6 7

4
9
15
22


printf '%s\n' "$@" | paste -sd+ - | bc

Regards,
Alister