Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get all the indexes of an array in Shell

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report