shell 05 (shell index array variable)

1. Array

The shell supports arrays. An array is a collection of data, and each piece of data is called an element of the array.

Note that the Bash shell only supports one-dimensional arrays, not multi-dimensional arrays.

In the Shell, brackets ( ) are used to represent arrays, and array elements are separated by spaces.

The syntax is:

array_name=(arr1 arr2 ...)  #方式一
array_name=([索引下标]=arr1 [索引下标]=arr2 ...) #方式二

1.1 Array acquisition

1. Get the element value through subscript, index starts from 0

${arr[index]}
${#arr[@]}
${#arr[*]}



2. Get the value and copy it to other variables at the same time

item=${arr[index]}



3. Use @ or * to get all elements in the array

${arr[@]}
${arr[*]}


4. Get the length or number of arrays

${#arr[@]}
${#arr[*]}

5. Get the characters of the specified element in the array

${#arr[索引]}

1.2 Array splicing

The so-called Shell array splicing (array merging) is to connect two arrays into one array

1.3 Array deletion

Delete the specified element data of the array and delete the entire array data

Guess you like

Origin blog.csdn.net/peng_258/article/details/132449822