|
Arrays in Shell Scripting |
|
|
|
Page 1 of 9 Arrays in Shell Scripting Newer versions of Bash support one-dimensional arrays. Array elements may be initialized with the variable[xx] notation. Alternatively, a script may introduce the entire array by an explicit declare -a variable statement. To dereference (retrieve the contents of) an array element, use curly bracket notation, that is, ${element[xx]}.
Example 26-1. Simple array usage #!/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.
Example 26-2. Formatting a poem #!/bin/bash # poem.sh: Pretty-prints one of the document 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."
# 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.
|