This article mainly introduces the Shell array and associated array details and information about the instance code. For more information, see Shell array and associated array.
1. array
1.1. array definition
An array is represented by parentheses, and elements in the array are separated by spaces.
xiaosi@Qunar:~$ a=(1 2 3)xiaosi@Qunar:~$ echo $a1xiaosi@Qunar:~$ a=("yoona" "lucy" "tom")xiaosi@Qunar:~$ echo $ayoona
1.2. array length, element, value assignment and deletion
Length: use $ {# array name [@ or *]} to obtain the array length.
xiaosi@Qunar:~$ echo ${#a[@]}3xiaosi@Qunar:~$ echo ${#a[*]}3
Get element: use $ {array name [subscript]} to get the array element (subscript starts from 0), subscript is * or @ to get the entire array content
xiaosi@Qunar:~$ array=("yoona" "lucy" "tom")xiaosi@Qunar:~$ echo ${array[0]}yoonaxiaosi@Qunar:~$ echo ${array[1]}lucyxiaosi@Qunar:~$ echo ${array[*]}yoona lucy tomxiaosi@Qunar:~$ echo ${array[@]}yoona lucy tom
Assign value: the array name [subscript] can be used to reference and assign values. if the subscript does not exist, a new array element is automatically added.
xiaosi@Qunar:~$ array=("yoona" "lucy" "tom")xiaosi@Qunar:~$ echo ${array[2]}tomxiaosi@Qunar:~$ array[2]=lilyxiaosi@Qunar:~$ echo ${array[2]}lily
Delete: The unset array [subscript] can be used to clear the corresponding elements. all data is cleared without a subscript.
xiaosi@Qunar:~$ array=("yoona" "lucy" "tom")xiaosi@Qunar:~$ unset array[1]xiaosi@Qunar:~$ echo ${array[*]}yoona tomxiaosi@Qunar:~$ unset arrayxiaosi@Qunar:~$ echo ${array[*]}xiaosi@Qunar:~$
1.3. obtain elements in a certain range
Use $ {array name [@ or *]: start position: length} to obtain elements in the specified range of the array. return strings separated by spaces.
xiaosi@Qunar:~$ array=(yoona lucy tom)xiaosi@Qunar:~$ echo ${array[*]}yoona lucy tomxiaosi@Qunar:~$ echo ${array[*]:1:2}lucy tomxiaosi@Qunar:~$ echo ${array[@]:0:1}yoona
1.4. replace
$ {Array name [@ or *]/find character/replace character} this operation does not change the original array content. if you need to modify it, refer to the above example.
xiaosi@Qunar:~$ array=(yoona lucy tom)xiaosi@Qunar:~$ echo ${array[@]/lucy/lily}yoona lily tomxiaosi@Qunar:~$ echo ${array[@]}yoona lucy tom
2. join an array
Bash supports associating arrays. it can use strings as array indexes. sometimes it is easier to understand using string indexes.
2.1 define correlated arrays
First, declare a variable as an associated array using the declaration statement.
xiaosi@Qunar:~$ declare -A assArray
After the element is declared, you can add the element to the joined array in two ways.
(1) using the embedded index-value list method
xiaosi@Qunar:~$ assArray=([lucy]=beijing [yoona]=shanghai)xiaosi@Qunar:~$ echo ${assArray[lucy]}beijing
(2) assign values using independent index-values
xiaosi@Qunar:~$ assArray[lily]=shandongxiaosi@Qunar:~$ assArray[sunny]=xianxiaosi@Qunar:~$ echo ${assArray[sunny]}xianxiaosi@Qunar:~$ echo ${assArray[lily]}shandong
2.2 List array indexes
Each array has an index for search. Use $ {! Array name [@ or *]} to obtain the index list of the array
xiaosi@Qunar:~$ echo ${!assArray[*]}lily yoona sunny lucyxiaosi@Qunar:~$ echo ${!assArray[@]}lily yoona sunny lucy
2.3 obtain all key-value pairs
#! /bin/bashdeclare -A cityArraycityArray=([yoona]=beijing [lucy]=shanghai [lily]=shandong)for key in ${!cityArray[*]}do echo "${key} come from ${cityArray[$key]}"done
Result:
xiaosi@Qunar:~/company/sh$ bash array.shlily come from shandongyoona come from beijinglucy come from shanghai
Thank you for reading this article. I hope it will help you. thank you for your support for this site!
For more details about Shell arrays and associated arrays and related articles on instance code, please refer to PHP!