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 understand Linux Shell script array and associative array

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to understand Linux Shell script arrays and associative arrays". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to understand Linux Shell script arrays and associative arrays.

I. Array and associative array

Arrays are a very important part of Shell scripts, which store multiple independent data as a collection with the help of indexes. Ordinary arrays can only use integers as indexes, while associative arrays can use not only integers as indexes, but also strings as indexes. In general, it is easier for people to understand using strings as indexes. Bash has introduced associative arrays since 4. 0.

Second, define the print ordinary array

The methods of arrays are as follows:

The code is as follows:

# list all elements on one line

Array_var= (1 2 3 4 5 6)

# list them one by one in the form of "index-value"

Array_var [0] = "test1"

Array_var [1] = "test2"

Array_var [2] = "test3"

Note: the first method should use parentheses, otherwise an error will be reported later.

There are several methods for array elements:

The code is as follows:

Echo ${array_var [0]} # output result is test1

Index=2

Echo ${array_var [$index]} # output result is test3

Echo ${array_var [*]} # outputs all array elements

Echo ${array_var [@]} # outputs all array elements

Echo ${# array_var [*]} # output value is 3

Note: in ubuntu 14.04, the shell script starts with #! / bin/bash, and the script is executed as bash test.sh.

Define the print associative array

Define an associative array

In an associative array, you can use any text as an array index. When defining an associative array, you first need to declare a variable as an associative array using a declaration statement before you can add elements to the array, as follows:

The code is as follows:

Declare-An ass_array # declares an associative array

Ass_array= (["index1"] = index1 ["index2"] = index2) # embedded "index-value" listing method

Ass_array ["index3"] = index3

Ass_array ["index4"] = index4

Echo ${ass_array ["index1"]} # output to index1

Echo ${ass_array ["index4"]}

Echo ${! ass_array [*]} # output index list

Echo ${! ass_array [@]} # output index list

Note: for normal arrays, you can still list the index list using the above method, and you cannot add the dollar sign $before declaring associative arrays and adding array elements

At this point, I believe you have a deeper understanding of "how to understand Linux Shell script arrays and associative arrays". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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