1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-14 08:13:24 -07:00
rc/sh/sum

26 lines
486 B
Plaintext
Raw Normal View History

2021-07-29 00:37:35 -07:00
#!/usr/bin/env sh
# YES_ZSH
# YES_BASH
# YES_DASH
2021-09-23 06:48:05 -07:00
# YES_ASH
2021-07-29 00:37:35 -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.
2021-07-29 00:37:35 -07:00
local sum=0
for i; do
i="${i%,}"
: $((sum+=i))
2021-07-29 00:37:35 -07:00
done
echo "$sum"
}
[ -n "${preload+-}" ] || sum "$@"