Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get all the indexes of an array in Shell

Shulou Source: shulou.com Published: 2022-06-02 05:37:31 11月12日 Update

This article mainly introduces "how to get all the indexes of the array in Shell". In daily operation, I believe many people have doubts about how to get all the indexes of the array in Shell. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts of "how to get all the indexes of the array in Shell"! Next, please follow the small series to learn together!

Array is a very important part of Shell, which stores multiple independent data as a collection by means of index. Arrays are divided into ordinary arrays and associative arrays. Ordinary arrays can only use integers as array indexes, while associative arrays can use strings as array indexes.

Definition of array

Arrays are represented by a pair of parentheses, and array elements are separated by space symbols, as follows:

array1=(1 2 3)

array2=("xiaoqian" "xiaofeng")

In addition, an array can be defined as a set of index-values, as follows:

array3[0]=1

array3[1]=2

array3[4]=3

Note that index values can be discontinuous and have unlimited range.

The above is the definition of an ordinary array. Before defining an associative array, you first need to use a declaration statement to declare it, as follows:

declare -A array4

array4=([xiaoqian]=18 [xiaofeng]=19)

declare -A array5

array5[xiaoqian]=18

array5[xiaofeng]=20

Operations on arrays

Once the array definition is complete, you can do something with it, as follows:

1. Get array length

echo ${#array1[*]} #Output 3

echo ${#array4[@]} #Output 2

Both of these methods can get the length of an array, similar to the way you get the length of a string. In addition, the length of a single element in an array can be obtained using the following methods:

echo ${#array2[0]} #Output 8

echo ${#array5[xiaoqian]} #Output 2

2. Get array element values

echo ${array1[0]} #Output 1

echo ${array4[xiaoqian]} #Output 18

The above statement is to get the value of a single element in the array. If you need to get all the elements in the array, you can use the @ or * symbol, as follows:

echo ${array1[*]} #Output 1 2 3

echo ${array4[@]} #Output 19 18

3. Get array index

echo ${! array1[*]} #Output 0 1 2

echo ${! array4[@]} #output xiaofeng xiaoqian

At this point, the study of "how to get all the indexes of an array in Shell" is over, hoping to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Tags: Array index output element method length learning normal association single character string more symbol statement help practical important next between Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi NVidia Shulou Information Docker