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. |
About AquaClusters Privacy Policy Support Version - 19.0.2-4 AquaFold, Inc Copyright © 2007-2017