I have the following code, that I want to be inside an function getsum(). I tried with the following code working without the function. When I run ./sum 5 6 I get 11.
#!/bin/bash
sum=0
for i in $@; do sum=$((sum+i)); done
echo $sum
exit 0
But how can I put it in a function doing the same job?
I tried the following code but it doesn't work.
#!/bin/bash
sums() {
sum=0
for i in $@; do sum=$((sum+i)); done
echo $sum
exit 0
}
sums
exit 0your function looks fine. That statement will terminate your shell!