Shell Array related Operations

Source: Internet
Author: User

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

Create an array

A= (12345)

Get array length

${#a [@]} #${#变量} to get variable length ${#a [*]}

Print array Contents

Echo ${a[@]}    #echo $a print only the first element of the array echo ${a[*]}

Delete

unset a[1]    #删除单个成员unset a        # delete entire array

Single Assignment

a[1]=a[]=#下标值超出长度索引, when the length of the array A is 5+1=6,a[5],a[6],a[7],a[8],a[9] no value, Print the last contents of the array: Echo ${A[10]}

Sharding

Echo ${a[@]:1:2}    #格式: ${variable name [@ or *]: Start index: Shard length}
C= (${a[*]:1:2}) #若不用括号扩起来得到的是字符串
C=${a[*]:1:2} #此时, C is a string

Replace

echo ${a[@]/2/$     #不会改变原数组ay=${a[*]/2/$}         # At this point, Y is the string y= (${a[@]/2/$})     #此时, y is an array

Shell Array related Operations

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.