Shell Script Raiders (learning notes)--1.7 array

Source: Internet
Author: User

There are two types of arrays in bash: normal arrays and associative arrays. An ordinary array can use integer values only as an array index, and associative arrays may use strings as indexes.

1.7.1 Normal Array

Define an array in one way:

[Email protected] tmp]# array_test= (1 2 3 4)

They are stored in the position of index bit 0-3, respectively, array_test[0] to array_test[3] corresponding values.

[[email protected "tmp ]# echo ${array_test[2]} ç How arrays are referenced Span lang= "en-US" >:${array_name[index]}

3

Note that the definition in the array is defined in parentheses using spaces as delimiters instead of commas. If you use commas, they will act as a whole, which is the value of array index 0.

If you use commas, then:

[email protected] tmp]# array_test= (1,2,3,4)   

[[email protected "tmp ]# echo ${array_test[0]} ç overall result as index bit.

1,2,3,4  

How to define an array two: You can customize the index bit.

[Email protected] tmp]# array_test[1]=1

[Email protected] tmp]# array_test[2]=2

[Email protected] tmp]# array_test[3]=3

[Email protected] tmp]# array_test[4]=4

[[email protected] tmp]# echo ${array_test[*]}

1,2,3,4 1 2 3 4

Prints all the values of the array.

[[email protected] tmp]# echo ${array_test[*]}

1,2,3,4 1 2 3 4

Or:

[[email protected] tmp]# echo ${array_test[@]}

1,2,3,4 1 2 3 4

View the array index number.

[[email protected] tmp]# echo ${!array_test[*]}

0 1 2) 3 4

Or

[[email protected] tmp]# echo ${!array_test[@]}

0 1 2) 3 4

1.7.2 Statistic Array length

[[email protected] tmp]# echo ${#array_test [*]}

5

[[email protected] tmp]# echo ${#array_test [@]}

5

1.7.3 Associative arrays

Associative arrays support strings as an array index. Using an associative array must first declare it with DECLARE-A.

[[email protected "tmp ]# declare-a array_dep ç after declaration, it can be assigned a value

[Email protected] tmp]# array_dep= ([Name1]=longshuai [Name2]=xiaofang]

[[email protected] tmp]# echo ${array_dep[name1]}

Longshuai

You can also assign values separately.

[Email protected] tmp]# Array_dep[name3]=zhangsan

[Email protected] tmp]# Array_dep[name4]=lisi

[[email protected] tmp]# echo ${array_dep[name4]}

Lisi

View all values of the array.

[[email protected] tmp]# echo ${array_dep[*]}

Zhangsan Xiaofang Longshuai Lisi C you can see the letters in reverse order.

Or:

[[email  Protected] tmp]# echo ${array_dep[@]}

Zhangsan Xiaofang Longshuai Lisi C you can see the letters in reverse order.

View the array index number.

[[email protected "tmp ]# echo ${!array_dep[@]}   ç reverse alphabetical order

Name3 name2 name1 Name4

Or:

[[email protected] tmp]# echo ${!array_dep[*]}

Name3 name2 name1 Name4

The statistic array length.

[[email protected] tmp]# echo ${#array_dep [*]}

4

Or:

[[email protected] tmp]# echo ${#array_dep [@]}

4

Shell Script Raiders (learning notes)--1.7 array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.