multiple values can be stored in an array. The Bash Shell supports only one-dimensional arrays (which do not support multidimensional arrays), and the subscript for array elements starts with 0.
The Shell array is represented by parentheses, and the elements are separated by a "space" symbol, with the following syntax:
Array_name= (value1 ... valuen)
Reading an array
# VI test.sh
#!/bin/bash
Array1= (a B c d)
echo "first element: ${array1[0]}"
echo "second element: ${array1[1]}"
echo "third element: ${array1[2]}"
echo "fourth element: ${array1[3]}"
Output:
# sh test.sh
First element: A
Second element: b
A third element: C
Fourth element: D
Gets all the elements in the array:
# VI test.sh
#!/bin/bash
Array1[0]=a
Array1[1]=b
Array1[2]=c
Array1[3]=d
echo "Elements of array: ${array1[*]}"
echo "Elements of array: ${array1[@]}"
Output:
# sh test.sh
Elements of an array: a B c D
Elements of an array: a B c D
Gets the number of elements in the array:
# VI test.sh
#!/bin/bash
Array1[0]=a
Array1[1]=b
Array1[2]=c
Array1[3]=d
echo "Number of elements of array: ${#array1 [*]}"
echo "Number of elements of array: ${#array1 [@]}"
Output:
# sh test.sh
Number of elements in array: 4
Number of elements in array: 4
Shell script from getting started to complex Quad (array)