Posted By

mrAlexGray on 04/10/11


Tagged


Versions (?)

Simple array usage


 / Published in: Bash
 

  1.  
  2. #!/bin/bash
  3.  
  4.  
  5. area[11]=23
  6. area[13]=37
  7. area[51]=UFOs
  8.  
  9. # Array members need not be consecutive or contiguous.
  10.  
  11. # Some members of the array can be left uninitialized.
  12. # Gaps in the array are okay.
  13. # In fact, arrays with sparse data ("sparse arrays")
  14. #+ are useful in spreadsheet-processing software.
  15.  
  16.  
  17. echo -n "area[11] = "
  18. echo ${area[11]} # {curly brackets} needed.
  19.  
  20. echo -n "area[13] = "
  21. echo ${area[13]}
  22.  
  23. echo "Contents of area[51] are ${area[51]}."
  24.  
  25. # Contents of uninitialized array variable print blank (null variable).
  26. echo -n "area[43] = "
  27. echo ${area[43]}
  28. echo "(area[43] unassigned)"
  29.  
  30. echo
  31.  
  32. # Sum of two array variables assigned to third
  33. area[5]=`expr ${area[11]} + ${area[13]}`
  34. echo "area[5] = area[11] + area[13]"
  35. echo -n "area[5] = "
  36. echo ${area[5]}
  37.  
  38. area[6]=`expr ${area[11]} + ${area[51]}`
  39. echo "area[6] = area[11] + area[51]"
  40. echo -n "area[6] = "
  41. echo ${area[6]}
  42. # This fails because adding an integer to a string is not permitted.
  43.  
  44. echo; echo; echo
  45.  
  46. # -----------------------------------------------------------------
  47. # Another array, "area2".
  48. # Another way of assigning array variables...
  49. # array_name=( XXX YYY ZZZ ... )
  50.  
  51. area2=( zero one two three four )
  52.  
  53. echo -n "area2[0] = "
  54. echo ${area2[0]}
  55. # Aha, zero-based indexing (first element of array is [0], not [1]).
  56.  
  57. echo -n "area2[1] = "
  58. echo ${area2[1]} # [1] is second element of array.
  59. # -----------------------------------------------------------------
  60.  
  61. echo; echo; echo
  62.  
  63. # -----------------------------------------------
  64. # Yet another array, "area3".
  65. # Yet another way of assigning array variables...
  66. # array_name=([xx]=XXX [yy]=YYY ...)
  67.  
  68. area3=([17]=seventeen [24]=twenty-four)
  69.  
  70. echo -n "area3[17] = "
  71. echo ${area3[17]}
  72.  
  73. echo -n "area3[24] = "
  74. echo ${area3[24]}
  75. # -----------------------------------------------
  76.  
  77. exit 0
  78.  
  79. As we have seen, a convenient way of initializing an entire array is the array=( element1 element2 ... elementN ) notation.
  80.  
  81.  
  82. Bash permits array operations on variables, even if the variables are not explicitly declared as arrays.
  83.  
  84. string=abcABC123ABCabc
  85. echo ${string[@]} # abcABC123ABCabc
  86. echo ${string[*]} # abcABC123ABCabc
  87. echo ${string[0]} # abcABC123ABCabc
  88. echo ${string[1]} # No output!
  89. # Why?
  90. echo ${#string[@]} # 1
  91. # One element in the array.
  92. # The string itself.
  93.  
  94. # Thank you, Michael Zick, for pointing this out.
  95. Once again this demonstrates that Bash variables are untyped.
  96.  
  97. Example 27-2. Formatting a poem
  98.  
  99. #!/bin/bash
  100. # poem.sh: Pretty-prints one of the ABS Guide author's favorite poems.
  101.  
  102. # Lines of the poem (single stanza).
  103. Line[1]="I do not know which to prefer,"
  104. Line[2]="The beauty of inflections"
  105. Line[3]="Or the beauty of innuendoes,"
  106. Line[4]="The blackbird whistling"
  107. Line[5]="Or just after."
  108. # Note that quoting permits embedding whitespace.
  109.  
  110. # Attribution.
  111. Attrib[1]=" Wallace Stevens"
  112. Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\""
  113. # This poem is in the Public Domain (copyright expired).
  114.  
  115. echo
  116.  
  117. tput bold # Bold print.
  118.  
  119. for index in 1 2 3 4 5 # Five lines.
  120. do
  121. printf " %s\n" "${Line[index]}"
  122. done
  123.  
  124. for index in 1 2 # Two attribution lines.
  125. do
  126. printf " %s\n" "${Attrib[index]}"
  127. done
  128.  
  129. tput sgr0 # Reset terminal.
  130. # See 'tput' docs.
  131.  
  132. echo
  133.  
  134. exit 0
  135.  
  136. # Exercise:
  137. # --------
  138. # Modify this script to pretty-print a poem from a text data file.
  139.  
  140. Array variables have a syntax all their own, and even standard Bash commands and operators have special options adapted for array use.
  141.  
  142. Example 27-3. Various array operations
  143.  
  144. #!/bin/bash
  145. # array-ops.sh: More fun with arrays.
  146.  
  147.  
  148. array=( zero one two three four five )
  149. # Element 0 1 2 3 4 5
  150.  
  151. echo ${array[0]} # zero
  152. echo ${array:0} # zero
  153. # Parameter expansion of first element,
  154. #+ starting at position # 0 (1st character).
  155. echo ${array:1} # ero
  156. # Parameter expansion of first element,
  157. #+ starting at position # 1 (2nd character).
  158.  
  159. echo "--------------"
  160.  
  161. echo ${#array[0]} # 4
  162. # Length of first element of array.
  163. echo ${#array} # 4
  164. # Length of first element of array.
  165. # (Alternate notation)
  166.  
  167. echo ${#array[1]} # 3
  168. # Length of second element of array.
  169. # Arrays in Bash have zero-based indexing.
  170.  
  171. echo ${#array[*]} # 6
  172. # Number of elements in array.
  173. echo ${#array[@]} # 6
  174. # Number of elements in array.
  175.  
  176. echo "--------------"
  177.  
  178. array2=( [0]="first element" [1]="second element" [3]="fourth element" )
  179. # ^ ^ ^ ^ ^ ^ ^ ^ ^
  180. # Quoting permits embedding whitespace within individual array elements.
  181.  
  182. echo ${array2[0]} # first element
  183. echo ${array2[1]} # second element
  184. echo ${array2[2]} #
  185. # Skipped in initialization, and therefore null.
  186. echo ${array2[3]} # fourth element
  187. echo ${#array2[0]} # 13 (length of first element)
  188. echo ${#array2[*]} # 3 (number of elements in array)
  189.  
  190. exit
  191.  
  192. Many of the standard string operations work on arrays.
  193.  
  194.  

Report this snippet  

You need to login to post a comment.