Title: Numerical N accumulates to M (n<m)
In the following example, the examples are 1 to 100, and they can be brought into N and M (n<m) entirely by themselves.
For loop implementation
Sum=0for ((i=1;i<=100;i++)) do sum=$ ((i+ $sum)) Doneecho $sum
By using variable arithmetic to implement
Echo $[$ (Echo +{1..100})]echo $ (($ (Echo +{1..100})) echo $[(100+1) * (100/2)]
Knowledge Points:
I=2&&echo $ ((i+3)) # Prints new variable results after calculation I=2&&echo $[i+3]# calculation of new variable results
$ (()) and $[] As a result, the two variables are expressed in different forms, but the results are the same
Implemented with SEQ and BC commands
Seq-s ' + ' |BC
Knowledge Points:
Seq Common parameter # does not specify the starting value, the default is 1-s # option mainly change the output of the splitter, the preset is \n-w # equal complement, that is, the width equals, insufficient front fill 0-f # formatted output, is the format of the specified printing
Thank:
Learn how to loosely summarize Linux
Old boy teacher's blog share
[Boiled water]-shell-from numerical N to M (n<m)-Knowledge point