Easily find issues by searching: #<Issue ID>
Example: #1832
Easily find members by searching in: <username>, <first name> and <last name>.
Example: Search smith, will return results smith and adamsmith
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 27-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.
base64_charset=( {A..Z} {a..z} {0..9} + / = ) # Using extended brace expansion #+ to initialize the elements of the array. # Excerpted from vladz's "base64.sh" script #+ in the "Contributed Scripts" appendix.
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.
Example 27-4. String operations on arrays
#!/bin/bash # array-strops.sh: String operations on arrays. # Script by Michael Zick. # Used in ABS Guide with permission. # Fixups: 05 May 08, 04 Aug 08. # In general, any string operation using the ${name ... } notation #+ can be applied to all string elements in an array, #+ with the ${name[@] ... } or ${name[*] ...} notation. arrayZ=( one two three four five five ) echo # Trailing Substring Extraction echo ${arrayZ[@]:0} # one two three four five five # ^ All elements. echo ${arrayZ[@]:1} # two three four five five # ^ All elements following element[0]. echo ${arrayZ[@]:1:2} # two three # ^ Only the two elements after element[0]. echo "---------" # Substring Removal # Removes shortest match from front of string(s). echo ${arrayZ[@]#f*r} # one two three five five # ^ # Applied to all elements of the array. # Matches "four" and removes it. # Longest match from front of string(s) echo ${arrayZ[@]##t*e} # one two four five five # ^^ # Applied to all elements of the array. # Matches "three" and removes it. # Shortest match from back of string(s) echo ${arrayZ[@]%h*e} # one two t four five five # ^ # Applied to all elements of the array. # Matches "hree" and removes it. # Longest match from back of string(s) echo ${arrayZ[@]%%t*e} # one two four five five # ^^ # Applied to all elements of the array. # Matches "three" and removes it. echo "----------------------" # Substring Replacement # Replace first occurrence of substring with replacement. echo ${arrayZ[@]/fiv/XYZ} # one two three four XYZe XYZe # ^ # Applied to all elements of the array. # Replace all occurrences of substring. echo ${arrayZ[@]//iv/YY} # one two three four fYYe fYYe # Applied to all elements of the array. # Delete all occurrences of substring. # Not specifing a replacement defaults to 'delete' ... echo ${arrayZ[@]//fi/} # one two three four ve ve # ^^ # Applied to all elements of the array. # Replace front-end occurrences of substring. echo ${arrayZ[@]/#fi/XY} # one two three four XYve XYve # ^ # Applied to all elements of the array. # Replace back-end occurrences of substring. echo ${arrayZ[@]/%ve/ZZ} # one two three four fiZZ fiZZ # ^ # Applied to all elements of the array. echo ${arrayZ[@]/%o/XX} # one twXX three four five five # ^ # Why? echo "-----------------------------" replacement() { echo -n "!!!" } echo ${arrayZ[@]/%e/$(replacement)} # ^ ^^^^^^^^^^^^^^ # on!!! two thre!!! four fiv!!! fiv!!! # The stdout of replacement() is the replacement string. # Q.E.D: The replacement action is, in effect, an 'assignment.' echo "------------------------------------" # Accessing the "for-each": echo ${arrayZ[@]//*/$(replacement optional_arguments)} # ^^ ^^^^^^^^^^^^^ # !!! !!! !!! !!! !!! !!! # Now, if Bash would only pass the matched string #+ to the function being called . . . echo exit 0 # Before reaching for a Big Hammer -- Perl, Python, or all the rest -- # recall: # $( ... ) is command substitution. # A function runs as a sub-process. # A function writes its output (if echo-ed) to stdout. # Assignment, in conjunction with "echo" and command substitution, #+ can read a function's stdout. # The name[@] notation specifies (the equivalent of) a "for-each" #+ operation. # Bash is more powerful than you think!
Command substitution can construct the individual elements of an array.
Example 27-5. Loading the contents of a script into an array
#!/bin/bash # script-array.sh: Loads this script into an array. # Inspired by an e-mail from Chris Martin (thanks!). script_contents=( $(cat "$0") ) # Stores contents of this script ($0) #+ in an array. for element in $(seq 0 $((${#script_contents[@]} - 1))) do # ${#script_contents[@]} #+ gives number of elements in the array. # # Question: # Why is seq 0 necessary? # Try changing it to seq 1. echo -n "${script_contents[$element]}" # List each field of this script on a single line. # echo -n "${script_contents[element]}" also works because of ${ ... }. echo -n " -- " # Use " -- " as a field separator. done echo exit 0 # Exercise: # -------- # Modify this script so it lists itself #+ in its original format, #+ complete with whitespace, line breaks, etc.
In an array context, some Bash builtins have a slightly altered meaning. For example, unset deletes array elements, or even an entire array.
Example 27-6. Some special properties of arrays
#!/bin/bash declare -a colors # All subsequent commands in this script will treat #+ the variable "colors" as an array. echo "Enter your favorite colors (separated from each other by a space)." read -a colors # Enter at least 3 colors to demonstrate features below. # Special option to 'read' command, #+ allowing assignment of elements in an array. echo element_count=${#colors[@]} # Special syntax to extract number of elements in array. # element_count=${#colors[*]} works also. # # The "@" variable allows word splitting within quotes #+ (extracts variables separated by whitespace). # # This corresponds to the behavior of "$@" and "$*" #+ in positional parameters. index=0 while [ "$index" -lt "$element_count" ] do # List all the elements in the array. echo ${colors[$index]} # ${colors[index]} also works because it's within ${ ... } brackets. let "index = $index + 1" # Or: # ((index++)) done # Each array element listed on a separate line. # If this is not desired, use echo -n "${colors[$index]} " # # Doing it with a "for" loop instead: # for i in "${colors[@]}" # do # echo "$i" # done # (Thanks, S.C.) echo # Again, list all the elements in the array, but using a more elegant method. echo ${colors[@]} # echo ${colors[*]} also works. echo # The "unset" command deletes elements of an array, or entire array. unset colors[1] # Remove 2nd element of array. # Same effect as colors[1]= echo ${colors[@]} # List array again, missing 2nd element. unset colors # Delete entire array. # unset colors[*] and #+ unset colors[@] also work. echo; echo -n "Colors gone." echo ${colors[@]} # List array again, now empty. exit 0
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017