Array of Shell programming

Source: Internet
Author: User

Definition of 0x00 Array

The definition group of arrays has its place as a special data structure in any programming language, and of course the Bash shell is no exception. This article makes a small summary of the shell array. Here we only discuss the case of one-dimensional arrays, about multidimensional arrays (in fact, you have to use a one-dimensional array method to simulate), not involved. This includes copying, calculating, deleting, and replacing the array.

  How arrays are defined:

1. Array[key]=value # arr[0]= "Hello" arr[1]= "World"

2. array= (value1 value2 ...) # arr= ("Hello" "world") note that there are spaces in front of parentheses

3. array= ([1]=value1 [2]=value2) # arr= ([0]= "Hello" [1]= "World")

Access to the 0x01 array

-Single access to elements in an array

${array[key]} # ${arr[1]}

-All elements in the array

Using the ${array name [subscript]} subscript is starting from 0 subscript is: * or @ Get the entire array contents

${array[* or @]} #${arr[*]}

-Calculate the length of the array

Use ${#数组名 [@ or *]} to get the array length

${#array [*]}

-Extraction of arrays

Directly through the ${array name [@ or *]: Start position: Length} Slice the original array, return is a string, the middle is separated by "space", so if you add "()", you will get the slice array

Arr= (1 2 3 4 5 6 7)

${arr[@]:0} # represents all elements

${ARR[@]:1} # Remove all elements after the first element

${arr[@]:0:2} # from 0 onwards fetch 2

-Deletion of array elements

Directly through: unset array [subscript] can clear the corresponding element, without subscript, clear the entire data.

Unset Arr[1]

-Substitution of array elements

${array name [@ or *]/find character/replace character} The operation does not change the contents of the original array

Arr= (1 2 3 4 5)

Echo ${ARR[@]/2/10} # result is 1 10 3 4 5

Arr= (${ARR[@]/2/10}) # Change the contents of the original array

Echo $arr # 1 10 3 4 5

Array of Shell programming

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.