mirror of https://github.com/tLDP/LDP
44 lines
1.3 KiB
Bash
44 lines
1.3 KiB
Bash
#!/bin/bash
|
|
# array-ops.sh: More fun with arrays.
|
|
|
|
|
|
array=( zero one two three four five )
|
|
|
|
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]} # 4
|
|
# 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" )
|
|
|
|
echo ${array2[0]} # first element
|
|
echo ${array2[1]} # second element
|
|
echo ${array2[2]} #
|
|
# Skipped in initialization, and therefore null.
|
|
echo ${array2[3]} # fourth element
|
|
|
|
|
|
exit 0
|