Posted By

mrAlexGray on 04/10/11


Tagged


Versions (?)

Counting Increment


 / Published in: Bash
 

  1. +=
  2. plus-equal (increment variable by a constant)
  3.  
  4. let "var += 5" results in var being incremented by 5.
  5.  
  6. -=
  7. minus-equal (decrement variable by a constant)
  8.  
  9. *=
  10. times-equal (multiply variable by a constant)
  11.  
  12. let "var *= 4" results in var being multiplied by 4.
  13.  
  14. /=
  15. slash-equal (divide variable by a constant)
  16.  
  17. %=
  18. mod-equal (remainder of dividing variable by a constant)
  19.  
  20. Arithmetic operators often occur in an expr or let expression.
  21.  
  22.  
  23.  
  24. #!/bin/bash
  25. # Counting to 11 in 10 different ways.
  26.  
  27. n=1; echo -n "$n "
  28.  
  29. let "n = $n + 1" # let "n = n + 1" also works.
  30. echo -n "$n "
  31.  
  32.  
  33. : $((n = $n + 1))
  34. # ":" necessary because otherwise Bash attempts
  35. #+ to interpret "$((n = $n + 1))" as a command.
  36. echo -n "$n "
  37.  
  38. (( n = n + 1 ))
  39. # A simpler alternative to the method above.
  40. # Thanks, David Lombard, for pointing this out.
  41. echo -n "$n "
  42.  
  43. n=$(($n + 1))
  44. echo -n "$n "
  45.  
  46. : $[ n = $n + 1 ]
  47. # ":" necessary because otherwise Bash attempts
  48. #+ to interpret "$[ n = $n + 1 ]" as a command.
  49. # Works even if "n" was initialized as a string.
  50. echo -n "$n "
  51.  
  52. n=$[ $n + 1 ]
  53. # Works even if "n" was initialized as a string.
  54. #* Avoid this type of construct, since it is obsolete and nonportable.
  55. # Thanks, Stephane Chazelas.
  56. echo -n "$n "
  57.  
  58. # Now for C-style increment operators.
  59. # Thanks, Frank Wang, for pointing this out.
  60.  
  61. let "n++" # let "++n" also works.
  62. echo -n "$n "
  63.  
  64. (( n++ )) # (( ++n )) also works.
  65. echo -n "$n "
  66.  
  67. : $(( n++ )) # : $(( ++n )) also works.
  68. echo -n "$n "
  69.  
  70. : $[ n++ ] # : $[ ++n ] also works
  71. echo -n "$n "
  72.  
  73. #!/bin/bash
  74. # Counting to 11 in 10 different ways.
  75.  
  76. n=1; echo -n "$n "
  77.  
  78. let "n = $n + 1" # let "n = n + 1" also works.
  79. echo -n "$n "
  80.  
  81.  
  82. : $((n = $n + 1))
  83. # ":" necessary because otherwise Bash attempts
  84. #+ to interpret "$((n = $n + 1))" as a command.
  85. echo -n "$n "
  86.  
  87. (( n = n + 1 ))
  88. # A simpler alternative to the method above.
  89. # Thanks, David Lombard, for pointing this out.
  90. echo -n "$n "
  91.  
  92. n=$(($n + 1))
  93. echo -n "$n "
  94.  
  95. : $[ n = $n + 1 ]
  96. # ":" necessary because otherwise Bash attempts
  97. #+ to interpret "$[ n = $n + 1 ]" as a command.
  98. # Works even if "n" was initialized as a string.
  99. echo -n "$n "
  100.  
  101. n=$[ $n + 1 ]
  102. # Works even if "n" was initialized as a string.
  103. #* Avoid this type of construct, since it is obsolete and nonportable.
  104. # Thanks, Stephane Chazelas.
  105. echo -n "$n "
  106.  
  107. # Now for C-style increment operators.
  108. # Thanks, Frank Wang, for pointing this out.
  109.  
  110. let "n++" # let "++n" also works.
  111. echo -n "$n "
  112.  
  113. (( n++ )) # (( ++n )) also works.
  114. echo -n "$n "
  115.  
  116. : $(( n++ )) # : $(( ++n )) also works.
  117. echo -n "$n "
  118.  
  119. : $[ n++ ] # : $[ ++n ] also works
  120. echo -n "$n "
  121.  
  122. echo
  123.  
  124. a=2147483646
  125. echo "a = $a" # a = 2147483646
  126. let "a+=1" # Increment "a".
  127. echo "a = $a" # a = 2147483647
  128. let "a+=1" # increment "a" again, past the limit.
  129. echo "a = $a" # a = -2147483648
  130. # ERROR: out of range,
  131. # + and the leftmost bit, the sign bit,
  132. # + has been set, making the result negative.
  133.  
  134. echo
  135.  
  136. exit 0

Report this snippet  

You need to login to post a comment.