Revision: 44336
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 10, 2011 02:23 by mrAlexGray
Initial Code
#!/bin/bash area[11]=23 area[13]=37 area[51]=UFOs # Array members need not be consecutive or contiguous. # Some members of the array can be left uninitialized. # Gaps in the array are okay. # In fact, arrays with sparse data ("sparse arrays") #+ are useful in spreadsheet-processing software. echo -n "area[11] = " echo ${area[11]} # {curly brackets} needed. echo -n "area[13] = " echo ${area[13]} echo "Contents of area[51] are ${area[51]}." # Contents of uninitialized array variable print blank (null variable). echo -n "area[43] = " echo ${area[43]} echo "(area[43] unassigned)" echo # Sum of two array variables assigned to third area[5]=`expr ${area[11]} + ${area[13]}` echo "area[5] = area[11] + area[13]" echo -n "area[5] = " echo ${area[5]} area[6]=`expr ${area[11]} + ${area[51]}` echo "area[6] = area[11] + area[51]" echo -n "area[6] = " echo ${area[6]} # This fails because adding an integer to a string is not permitted. echo; echo; echo # ----------------------------------------------------------------- # Another array, "area2". # Another way of assigning array variables... # array_name=( XXX YYY ZZZ ... ) area2=( zero one two three four ) echo -n "area2[0] = " echo ${area2[0]} # Aha, zero-based indexing (first element of array is [0], not [1]). echo -n "area2[1] = " echo ${area2[1]} # [1] is second element of array. # ----------------------------------------------------------------- echo; echo; echo # ----------------------------------------------- # Yet another array, "area3". # Yet another way of assigning array variables... # array_name=([xx]=XXX [yy]=YYY ...) area3=([17]=seventeen [24]=twenty-four) echo -n "area3[17] = " echo ${area3[17]} echo -n "area3[24] = " echo ${area3[24]} # ----------------------------------------------- exit 0 As we have seen, a convenient way of initializing an entire array is the array=( element1 element2 ... elementN ) notation. Bash permits array operations on variables, even if the variables are not explicitly declared as arrays. string=abcABC123ABCabc echo ${string[@]} # abcABC123ABCabc echo ${string[*]} # abcABC123ABCabc echo ${string[0]} # abcABC123ABCabc echo ${string[1]} # No output! # Why? echo ${#string[@]} # 1 # One element in the array. # The string itself. # Thank you, Michael Zick, for pointing this out. Once again this demonstrates that Bash variables are untyped. Example 27-2. Formatting a poem #!/bin/bash # poem.sh: Pretty-prints one of the ABS Guide author's favorite poems. # Lines of the poem (single stanza). Line[1]="I do not know which to prefer," Line[2]="The beauty of inflections" Line[3]="Or the beauty of innuendoes," Line[4]="The blackbird whistling" Line[5]="Or just after." # Note that quoting permits embedding whitespace. # Attribution. Attrib[1]=" Wallace Stevens" Attrib[2]="\"Thirteen Ways of Looking at a Blackbird\"" # This poem is in the Public Domain (copyright expired). echo tput bold # Bold print. for index in 1 2 3 4 5 # Five lines. do printf " %s\n" "${Line[index]}" done for index in 1 2 # Two attribution lines. do printf " %s\n" "${Attrib[index]}" done tput sgr0 # Reset terminal. # See 'tput' docs. echo exit 0 # Exercise: # -------- # Modify this script to pretty-print a poem from a text data file. Array variables have a syntax all their own, and even standard Bash commands and operators have special options adapted for array use. Example 27-3. Various array operations #!/bin/bash # array-ops.sh: More fun with arrays. array=( zero one two three four five ) # Element 0 1 2 3 4 5 echo ${array[0]} # zero echo ${array:0} # zero # Parameter expansion of first element, #+ starting at position # 0 (1st character). echo ${array:1} # ero # Parameter expansion of first element, #+ starting at position # 1 (2nd character). echo "--------------" echo ${#array[0]} # 4 # Length of first element of array. echo ${#array} # 4 # Length of first element of array. # (Alternate notation) echo ${#array[1]} # 3 # Length of second element of array. # Arrays in Bash have zero-based indexing. echo ${#array[*]} # 6 # Number of elements in array. echo ${#array[@]} # 6 # Number of elements in array. echo "--------------" array2=( [0]="first element" [1]="second element" [3]="fourth element" ) # ^ ^ ^ ^ ^ ^ ^ ^ ^ # Quoting permits embedding whitespace within individual array elements. echo ${array2[0]} # first element echo ${array2[1]} # second element echo ${array2[2]} # # Skipped in initialization, and therefore null. echo ${array2[3]} # fourth element echo ${#array2[0]} # 13 (length of first element) echo ${#array2[*]} # 3 (number of elements in array) exit Many of the standard string operations work on arrays.
Initial URL
Initial Description
Initial Title
Simple array usage
Initial Tags
Initial Language
Bash