2021-07-29 00:37:35 -07:00
|
|
|
#!/usr/bin/env sh
|
2024-07-23 05:34:17 -07:00
|
|
|
# compat: +ash +bash +dash +hush -ksh +mksh +oksh +osh +posh +yash +zsh
|
2021-07-29 00:37:35 -07:00
|
|
|
|
2021-07-30 19:41:16 -07:00
|
|
|
sum() { ### @-
|
2021-08-01 07:09:21 -07:00
|
|
|
### compute the summation of its arguments without forking processes.
|
|
|
|
### this relies on the shell's built-in arithmetic operators.
|
|
|
|
###
|
|
|
|
### ```
|
|
|
|
### $ sum 1 2 3
|
|
|
|
### 6
|
|
|
|
### ```
|
|
|
|
###
|
|
|
|
### **TODO:** consider renaming because sum(1) already exists.
|
2022-03-06 13:12:17 -08:00
|
|
|
local sum=0 i=
|
2021-07-29 00:37:35 -07:00
|
|
|
for i; do
|
2022-03-05 17:40:40 -08:00
|
|
|
i="${i%,}"
|
2021-07-29 10:40:15 -07:00
|
|
|
: $((sum+=i))
|
2021-07-29 00:37:35 -07:00
|
|
|
done
|
|
|
|
echo "$sum"
|
|
|
|
}
|
|
|
|
|
2021-08-02 13:48:46 -07:00
|
|
|
[ -n "${preload+-}" ] || sum "$@"
|