Инициализация массивов

Массивы и коллекции / Массивы

#empty array
n1=()
#n1 is ()

#the elements are separated by spaces, not by commas:
#(1, 2, 3) would give the elements "1," "2," and "3"
n2=(1 2 3)
n3=("one" "two" "three")
#n3[2] is "three"

#elements by index: the indexes between them do not exist
n4=([1]=10 [3]=30)
#n4 is (10 30), its indexes are (1 3)

#an array from a range and from the lines of a command
n5=({1..5})
#n5 is (1 2 3 4 5)
mapfile -t n6 < <(seq 3 -1 1)
#n6 is (3 2 1)

#bash has no multidimensional arrays:
#an associative array with the keys "row,column" stands in for one
declare -A matrix=([0,0]=1 [0,1]=2 [1,0]=4 [1,1]=5)
#matrix[1,1] is 5

echo "n1 is (${n1[*]})"
echo "n2 is (${n2[*]})"
echo "n3[2] is ${n3[2]}"
echo "n4 is (${n4[*]}), its indexes are (${!n4[*]})"
echo "n5 is (${n5[*]})"
echo "n6 is (${n6[*]})"
echo "matrix[1,1] is ${matrix[1,1]}"