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.
bash
feature won't handle floating point values (eg: 3.5). For that you'd need to use a program such asawk
,python
,dc
,bc
orperl
. See @rici's answer. – NVRAM Nov 5 '13 at 17:12