Shell script array

bash supports one-dimensional arrays (multi-dimensional arrays are not supported) and does not limit the size of arrays.

Similar to C language, the subscript of array elements is numbered from 0. To get the elements in the array, the subscript should be used. The subscript can be an integer or an arithmetic expression, and its value should be greater than or equal to 0.

1. Defining arrays

In Shell, the array is represented by brackets, and the array elements are separated by the "space" symbol.

  • The general form of defining an array is: array name = (value 1 value 2... Value n)

For example:

array_name=(value0 value1 value2 value3)

perhaps

array_name=(

value0

value1

value2

value3

)

You can also define the components of an array separately:

array_name[0]=value0

array_name[1]=value1

array_name[n]=valuen

Continuous subscripts may not be used, and there is no limit to the scope of subscripts.

#!/bin/bash
array1=(1 2 3 7 9)

array2=(
54
76
)

array3[0]=98
array3[5]=100

2. Read array

  • The general format for reading array element values is ${array name [subscript]}

Note: no and no out of range exception will be reported, i.e. no query

For example:

valuen=${array_name[n]}

Use the @ or * symbol to get all the elements in the array, for example:

echo ${array_name[@]}

[root@hadoop01 shell]# vi array.sh
#!/bin/bash

array1=(1 2 3 7 9)

array2=(
11
33
54
76
)

array1[0]=111
array2[1]=222

array3[0]=98
array3[5]=100

echo "this is array1"
echo ${array1}
echo ${array1[0]}
echo ${array1[@]}
echo ${array1[*]}

echo "this is array2"
echo ${array2}
echo ${array2[1]}
echo ${array2[@]}
echo ${array2[*]}

echo "this is array3"
echo ${array3}
echo ${array3[1]}
echo ${array3[@]}
echo ${array3[*]}

[root@hadoop01 shell]# chmod a+x array.sh 
[root@hadoop01 shell]# ./array.sh 
this is array1
111
111
111 2 3 7 9
111 2 3 7 9
this is array2
11
222
11 222 54 76
11 222 54 76
this is array3
98

98 100
98 100
[root@hadoop01 shell]# 

3. Get the length of the array

The method to get the array length is the same as that to get the string length, for example:

#Get the number of array elements

length=${#array_name[@]}

Or

length=${#array_name[*]}

echo "-----length-----"
echo ${#array1[*]}
echo ${#array2[*]}
echo ${#array3[*]}


-----length-----
5
4
2

4. Gets the length of a single element of an array

lengthn=${#array_name[n]}

echo "---single length---"
echo ${#array1[0]}
echo ${#array2[1]}
echo ${#array3[5]}

---single length---
3
3
3

Complete exercise Code: (note in running mode)

[root@hadoop01 shell]# vi array.sh

#!/bin/bash

array1=(1 2 3 7 9)

array2=(
11
33
54
76
)

array1[0]=111
array2[1]=222

array3[0]=98
array3[5]=100

echo "this is array1"
echo ${array1}
echo ${array1[0]}
echo ${array1[@]}
echo ${array1[*]}

echo "this is array2"
echo ${array2}
echo ${array2[1]}
echo ${array2[@]}
echo ${array2[*]}

echo "this is array3"
echo ${array3}
echo ${array3[1]}
echo ${array3[@]}
echo ${array3[*]}

echo "-----length-----"
echo ${#array1[*]}
echo ${#array2[*]}
echo ${#array3[*]}

echo "---single length---"
echo ${#array1[0]}
echo ${#array2[1]}
echo ${#array3[5]}

[root@hadoop01 shell]# ./array.sh 
this is array1
111
111
111 2 3 7 9
111 2 3 7 9
this is array2
11
222
11 222 54 76
11 222 54 76
this is array3
98

98 100
98 100
-----length-----
5
4
2
---single length---
3
3
3

 

Keywords: shell C

Added by t_galan on Tue, 31 Dec 2019 22:51:13 +0200