Usage of shell arrays

Source: Internet
Author: User

Variable: Stores the memory space of a single element:
Array (multiple contiguous memory spaces): a contiguous memory space that stores multiple elements:

Array Name:
Index: Numbering starting from 0, is a numeric index;
Note: Indexes can also support the use of custom formats, not just numeric formats:
The bash array supports sparse formatting
Referencing an array of elements: ${array_name[index]}

Declaring an array:
Declare-a Array_Name
Declare-a array_name: Associative array: KV

Assignment of array elements:
(1) Assign only one element at a time:
Array_name[index]=value
#weekdays [0]= "Sunday"
#weekdays [4]= "Thursday"

(2) Assign all elements at once:
Array_name= ("VAL1", "VAL2", "VAL3",...)
(3) Assign only specific elements:
Array_name= ([0]= "VAL1" [3]= "VAL2" ...)
(4) Read-a ARRAY

To reference an element in an array:

${array_name[index]}
Note: Omitting [INDEX] means referencing an element with subscript 0

Displays all elements in the array:

${array_name[*]}, or ${array_name[@]}

The length of the statistic array (the number of elements in the array):
${#ARRAY_NAME [*]},${#ARRAY_NAME [@]}

To reference an element in an array:
Remove all elements in the array in turn: ${array[@]} or ${array[*]}

Array slices:

Remove an element of the specified length in the array: ${array[@]:offset:number}: Also known as array slicing
Offset: Number of elements to skip
Number: How many elements to remove
Example: #echo ${weekdays[@]:1:2}

Remove all elements after offset: ${array[@]:offset}
#echo ${weekdays[@]:1}
Append an element to the array:
array[${#ARRAY [*]}]
To delete an element in an array:
Unset Array[index]
Associative arrays:
Declare-a Array_Name
Array_name= ([index_name]= ' val1 ' [index_name2= ' val2 ' ...])

This article is from the "burning Years of Passion" blog, please be sure to keep this source http://liuzhengwei521.blog.51cto.com/4855442/1927663

Usage of shell arrays

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.