Bash Shell array variables

Source: Internet
Author: User

First, define the array variable

1. Grammar

(1) arrayname= (parameter1 parameter2 ...)

Example 1, define an array named name, with a value of Claire,obama,george

Name= (Claire Obama )

(2) Arrayname= (

Parameter1

Parameter2

...

)

Again we change example 1 to the second way

Name= (

Claire

Obama

George

)

Second, access to functions

(1) Subscript access

${ARRAYNAME[0]} Example 1 is Claire.

${ARRAYNAME[1]} Example 1 is Obama.

${ARRAYNAME[2]} Example 1 is George.

${arrayname[n]}

(2) @ and * access

${arrayname[@]} and ${arrayname[*]} get the elements in the array, example 1 is Claire Obama George

Third, get the number of arrays

(1) Get the number of the entire array

${#ArrayName [@]} and ${#ArrayName [*]} Example 1 is 3,

(2) Get the number of individual elements

${#ArrayName [0]} Example 1 is 5.

${#ArrayName [1]} Example 1 is 4.

${#ArrayName [2]} Example 1 is 5.

Iv. iterating through an array

(1) using while traversal

I=0

While [$i-lt ${#ArrayName [*]}]

Do

echo "parameter is: ${arrayname[$i]}"

((i++))

Done

(2) using for traversal

For parameter in ${arrayname[*]}

Do

echo "parameter is: $parameter"

Done

V. Emptying an array

(1) emptying a single array

arrayname[n]=

Note: When emptying a single array, the length of the array does not change

(2) Empty the entire array

1.unset Arrayname

2.arrayname=


Bash Shell array variables

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.