How to create and use Linux shell arrays _linux shell

Source: Internet
Author: User
Tags array definition array length arrays

The Linux shell is much more powerful in programming than in Windows batching, whether in loops or operations. There is no comparison of data types. The following is a summary of how an individual operates on an array while in use.

1. Array definition

[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[Chengmo@centos5 ~]$ Echo $a

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

2. Array reading and Assignment

Get the length:

[Chengmo@centos5 ~]$ echo ${#a [@]}
5


Array length can be obtained with ${#数组名 [@ or *]}

Read:

[Chengmo@centos5 ~]$ Echo ${a[2]} 
3

[chengmo@centos5 ~]$ echo ${a[*]} 
1 2 3 4 5  

With ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the whole array content

Assign value:

[Chengmo@centos5 ~]$ a[1]=100

[chengmo@centos5 ~]$ echo ${a[*]} 
1 3 4 5

[chengmo@centos5 ~]$ a[5]=100
   
    [chengmo@centos5 ~]$ Echo ${a[*]}

1 100 3 4 5 100

   

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

Delete:

[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[chengmo@centos5 ~]$ unset a
[chengmo@centos5 ~]$ echo ${a[*]}

[Chengmo@ce Ntos5 ~]$ a= (1 2 3 4 5)
[chengmo@centos5 ~]$ unset a[1]  
[chengmo@centos5 ~]$ echo ${a[*]} 
1 3 4 5
[che Ngmo@centos5 ~]$ echo ${#a [*]}
4

Direct through: unset array [subscript] You can clear the corresponding elements, without the subscript, clear the entire data.

3. Special use

Sharding:

[Chengmo@centos5 ~]$ a= (1 2 3 4 5)
[Chengmo@centos5 ~]$ Echo ${a[@]:0:3}
1 2 3
[Chengmo@centos5 ~]$ Echo ${a[@ ]:1:4}
2 3 4 5

[chengmo@centos5 ~]$ c= (${a[@]:1:4})
[Chengmo@centos5 ~]$ echo ${#c [@]}
4
[ Chengmo@centos5 ~]$ Echo ${c[*]} 
2 3 4 5


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 "()", you will get the slice array, above example: C is a new data.

Replace:

[Chengmo@centos5 ~]$ a= (1 2 3 4 5)  
[Chengmo@centos5 ~]$ Echo ${a[@]/3/100}
1 2 4
[chengmo@centos5 ~]$] echo ${a[@]}
1 2 3 4 5
[chengmo@centos5 ~]$ a= (${a[@]/3/100}) 
[Chengmo@centos5 ~]$ echo ${a[@]}   
1 2 1 00 4 5

Call method is: ${array name [@ or *]/lookup character/substitution character} This operation does not change the original array contents, if you need to modify, you can look at the example above to redefine the data.

From the above, you can find that the Linux shell array is already very powerful, the common operation is more than sufficient.

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.