The Linux Shell Array uses

Source: Internet
Author: User

Ubuntu12.04 TLS 64bit, bash 4.2.25

First, the definition

An array is a collection of some data, divided into two types

(1) ordinary arrays, only integers can be used as indexes

(2) Associative array , can use string as index, Bash 4.0 start support, legacy version does not support


Second, some operations of the array

(1) Definition and assignment of arrays

< normal array >

"1" Definition

Array name = ()

"2" Value assignment

1. Array name = (elem1 elem2 elem3 ...)

Each element is separated by a space

2. Array name [0] = "test1"

array name [1] = "Test2"

......


< associative arrays >

"1" Definition

Declare-a array Name

"2" Value assignment

1. Array name = ([index1] = val1 [Index2] = Val2 ...)

2. Array name [INDEX1] = Val1

Array name [INDEX2] = Val2

......


(2) To find the length of the array

${#数组名 [*]} or {#数组名 [@]}

(3) Get an array of indexed elements

${array name [index value]}

(4) Get all elements of an array

${array name [*]} or ${array name [@]}

(5) Substitution of array elements

${array name [*]/find character/substitution character} or ${array name [@]/find character/replace character}

Replace does not change the original array, to modify the original array, you need to re-assign the value of the arrays

(6) Deleting an array element

unset array name #删除整个数组 unset array name [index value] #删除指定的元素

(7) Slicing of arrays

${array Name [*]: Start position: length} or ${array name [@]: Start position: Length}

A string separated by a space is returned after the slice, plus () the sub-array after the slice is obtained


Iii. examples

    #!/bin/bash                  #定义普通数组并进行赋值     comm_arr= (1 2 3 4 5)          comm_arr2= ()     comm_arr2[0]=6     comm_arr2[1]=7    comm_arr2[2]=8        # Get the length of the normal array     echo  "comm_arr1 len is ${#comm_arr [*]}"      echo  "comm_arr2 len is ${#comm_arr2 [@]}"           #获得普通数组的所有元素     echo  "comm_arr1 all elem is : ${comm_ Arr[*]} "    echo " comm_arr2 all elem is :${comm_arr2[@ "}"           #通过索引获得元素     echo  "comm_arr[4] =  ${COMM_ARR[4]} "    echo  "Comm_arr[2] = ${comm_arr2[2"} "    echo " comm_arr[ 100]&NBSP;=&NBSP;${COMM_ARR[100]} "  #索引值不存在, but without error, the result is empty         # Substitution of elements in an ordinary array     echo  "3&NBSP;IN&NBSP;COMM_ARR&NBSP;IS&NBSP;REPLACE&NBSP;200&NBSP;:  ${comm_arr[*]/3/200} "    echo " comm_arr is ${comm_arr[*]} " # The original array has not been modified     comm_arr2= (${COMM_ARR2[@]/7/9})    #替换原数组     echo   "7&NBSP;IN&NBSP;COMM_ARR2&NBSP;IS&NBSP;REPLACE&NBSP;9&NBSP;AND&NBSP;MODIFY&NBSP;COMM_ARR2"      echo  "comm_arr2 is ${comm_arr2[*]}"          #普通数组的切片     echo  "Splite comm_arr 1-3 is : ${comm_arr[*]:1:3 } "    comm_arr_split= (${comm_arr[@]:2:3})   #获得从第三个位置子开始3个元素的子数组      echo  "Sub arr : len = ${#comm_arr_split [*]} , elems is : ${comm_arr_split[@]} "          #普通数组的删除     echo  "Del before,  comm_arr is ${comm_arr[*]} "    echo " Del a elem "     unset comm_arr[0]  #删除某个元素     echo  "del after,  Comm_arr is ${comm_arr[*]} "        echo " Del all  arr "    unset comm_arr  #删除整个数组     echo " del  After, comm_arr is ${comm_arr[*]} "  #为空               #定义关联数组并赋值     declare -A link_arr     link_arr[' apple ']= ' 10$ '     link_arr[orange]= ' 100Y '          declare -a&nbsP;link_arr2    link_arr2= ([age]=20 [name]=zhangsan [sex]=m)           #获得关联数组的长度     echo  "link arr len: ${# Link_arr[*]} "    echo " link arr2 len: ${#link_arr2 [@]} "          #通过索引获得元素     echo  "Link arr index=apple ,  elem is ${link_arr[' apple '} '     echo  ' link arr2 index= Name, elem is ${link_arr2[name]} "    echo " link arr index= Name not exist, but no error, ${link_arr[name]} "  #虽然没有这个索引, but no error, just empty result          #输出关联数组中所有元素     echo  "Apple is  ${link_arr[*]} "    echo " link_arr2 is ${link_arr2[@ "}"         &nbSP; #关联数组中的替换     echo  "Link arr2 zhangsan is replace lisi,  ${link_arr2[*]/zhangsan/lisi} "  #原数组没有修改     echo " Link arr2 is  ${link_arr2[*]} "     #link_arr2 = (${link_arr2[*]/zhangsan/lisi})   #报错, associative array must use subscript      #echo   "link arr2 is ${link_arr2[*]}"      #echo   "Link arr2 name=${link_arr2[name]}"          #关联数组的切片     echo  "Link arr2 age-name: ${link_arr2[*]:name:2}"           #关联数组的删除     echo  "del before link  Arr2 is ${link_arr2[*]} "    unset link_arr2[age]     echo  "" del after link arr2 is ${link_arr2[*]}         unset link_arr    echo  "del all arr ${link_arr[*]}" 

Results:

COMM_ARR1 Len is 5
COMM_ARR2 Len is 3
COMM_ARR1 all Elem is:1 2 3 4 5
COMM_ARR2 all Elem Is:6 7 8
COMM_ARR[4] = 5
COMM_ARR[2] = 8
COMM_ARR[100] =
3 in Comm_arr is replace 200:1 2 200 4 5
Comm_arr is 1 2 3 4 5
7 in COMM_ARR2 is replace 9 and modify COMM_ARR2
COMM_ARR2 is 6 9 8
Splite Comm_arr 1-3 is:2 3 4
Sub Arr:len = 3, Elems is:3 4 5
Del before, Comm_arr is 1 2 3 4 5
Del a Elem
Del after, Comm_arr is 2 3 4 5
del all arr
Del after, Comm_arr is
Link arr Len:2
Link ARR2 Len:3
Link arr index=apple, Elem is 10$
Link arr2 index=name, Elem is Zhangsan
Link arr index=name not exist, but no error,
Apple is 100Y 10$
LINK_ARR2 is Zhangsan m
Link arr2 Zhangsan is replace Lisi, Lisi m
Link arr2 is Zhangsan m
Link ARR2 Age-name:zhangsan 20
Del before link arr2 is Zhangsan m
Del after link arr2 is Zhangsan m
del all arr

This article is from the "Snow Dancer" blog, please be sure to keep this source http://happytree007.blog.51cto.com/6335296/1711586

The Linux Shell Array uses

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.