I have the following shell script that sums up all the arguments passed to it (space separated):
sum=0
for arg in "$@"; do
(( sum += arg ))
done
echo $sum
I have saved it as SumAll.sh
. So, its supposed to function as follows:
sh SumAll.sh 2 4 6 8
20
or
bash SumAll.sh 1 -2 3 -3
-1
Both work on my local machine (MacBook), but when I try the same in a linux server, running the script with bash
works, but with sh
does not work. I get the following error:
sum: '+=': No such file or directory
sum: arg: No such file or directory
Why is that? And how do I fix it?
(( arith expr ))
is aksh88
extension and not supported by many shells. – schily May 12 '20 at 07:50