Linux shell array creation and usage tips

Source: Internet
Author: User
Tags array definition array length

Reference URL: http://www.cnblogs.com/chengmo/archive/2010/09/30/1839632.html

The Linux shell is much more powerful in programming than Windows batch processing, and cannot be compared in terms of loops, operations, and data types. Here is a summary of some of the operations in the array.

1. Array definition

[Email protected] ~]$ a= (1 2 3 4 5)
[Email protected] ~]$ echo $a
1

A pair of parentheses indicates an array, and the elements of the array are separated by a "space" symbol.

2. Array reading and Assignment

    • Get Length:

[[email protected] ~]$ echo ${#a [@]}
4

Use ${#数组名 [@ or *]} to get the array length

    • Read:

[[email protected] ~]$ echo ${a[0]}
1
[[email protected] ~]$ echo ${a[*]}
1 2 3 4

Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents

    • Assignment value:

[Email protected] ~]$ a[1]=100
[[email protected] ~]$ echo ${a[*]}
1 100 3 4
[Email protected] ~]$ a[3]=100
[[email protected] ~]$ echo ${a[*]}
1 100 3 100
[Email protected] ~]$ a[4]=100
[[email protected] ~]$ echo ${a[*]}
1 100 3) 100 100

It can be referenced directly by the array name [subscript], and a new array element is automatically added if the subscript does not exist.

    • Delete:

[Email protected] ~]$ a= (1 2 3 4 5)
[[email protected] ~]$ echo ${a[*]}
1 2 3) 4 5
[Email protected] ~]$ unset a
[[email protected] ~]$ echo ${a[*]}

[Email protected] ~]$ a= (1 2 3 4 5)
[Email protected] ~]$ unset a[1]
[[email protected] ~]$ echo ${a[*]}
1 3 4 5
[[email protected] ~]$ echo ${#a [*]}
4

Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.

3. Special use

    • Sharding:

[Email protected] ~]$ a= (1 2 3 4 5)
[[email protected] ~]$ echo ${a[*]:0:3}
1 2 3
[[email protected] ~]$ echo ${a[*]:1:4}
2 3 4 5
[[email protected] ~]$ c= (${a[*]:1:4})
[[email protected] ~]$ echo ${c[*]:1:4}
3 4 5
[[email protected] ~]$ c= (${a[*]:1:2})
[[email protected] ~]$ echo ${c[*]:1:1}
3

Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle with "space" separate, so if you add "()", will get the slice array, the above example: C is a new data.

    • Replace:

[Email protected] ~]$ a= (1 2 3 4 5)
[[email protected] ~]$ echo ${a[@]/3/100}
1 2 100) 4 5
[[email protected] ~]$ echo ${a[@]}
1 2 3) 4 5
[[email protected] ~]$ a= (${a[@]/3/100})
[[email protected] ~]$ echo ${a[@]}
1 2 100) 4 5

The call method is: ${array name [@ or *]/find character/substitution character} This operation will not change the original array contents, if you need to modify, you can see the above example is the parentheses assignment into an array, redefine the data.

Linux shell array creation and usage tips

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.