Bash – How to create a bash script that sums any number of command line arguments

arithmeticbashshell-script

How would I create a bash script that the user can use to sum any amount of command line arguments? For example, say my script is called sum:

sum 3

3

sum 3 5

8

sum 9 8 21

38

And so on.. I realize that I will have to loop through the command line arguments and increment them to the total, but I am not really sure how to do that. I am not sure how to reference say the 2nd command line argument to a specific variable or how to get the number of total command line arguments.

Best Answer

No need for bash, plain sh will do as well:

#! /bin/sh - 
IFS=+; echo "$(($*))"

$* in POSIX shells, expands to the list of positional parameters (in this case, the arguments to the script) separated by the first character of $IFS (or space if $IFS is unset or nothing if $IFS is empty). $((...)) is the shell internal arithmetic expansion operator (note that it supports decimal, octal and hexadecimal numbers)

If you need floating point support, that's where you'll need a different shell like ksh93 or zsh (not bash as bash only supports integer arithmetic), though you could also use awk:

#! /usr/bin/awk -f
BEGIN {t=0; for (i in ARGV) t+=ARGV[i]; print t}

That will use long (for integer) and double (for floating point) type numbers as implemented by your system. The input numbers must be decimal floating point or engineering notation in the English style (floating point delimiter is the period character regardless of the locale). With some awk implementations, it will fail if the first number is negative as awk would try to interpret it as an option.

Some awk implementations like GNU awk when POSIXLY_CORRECT is in the environment also support hexadecimals including with binary exponent notations. Or with --non-decimal-data, it understands octals and hexadecimals:

$ POSIXLY_CORRECT=1 ./sum  0xap3 0xa
90 # (0xa * 2^3) + 0xa
$ awk --non-decimal-data -f ./sum  010
8
Related Question