Linux array variables

Source: Internet
Author: User

The array definition in the shell:

You can define an array as a whole:

Array_name= (value0 value1 value2 value3 ...)

Or:

Array_name= (Value0value1value2value3 ...)

At this point the array's subscript defaults to 0, and the individual components of the array can be defined individually:

Array_name[0]=value0array_name[1]=value1array_name[n]=valuen ...

You can not use successive subscripts, and there is no limit to the range of subscripts.

Gets the elements in the array:

Valuen=${array_name[n]}

All the elements in the array are worth at once:

Echo ${array_name[@]}

gets the number of array elements :

length=${#ARRAY_NAME [@]} or length=${#ARRAY_NAME [*]}

gets the length of an array of individual elements :

lengthn=${#ARRAY_NAME [n]}

Array Traversal:

To iterate through an array using the while loop:

i=0length=${#ARRAY_NAME [*]}while [$i-lt $length]; dovaluei=${array_name[$i]}...let I++done

To iterate through an array with a For loop:

For value in ${array_name[*]}; Dovaluei=value...done

Empty the array:

To empty a single element:

array_name[n]=

Empty the entire array:

Unset array_name or: array_name=

It should be noted that the variable after unset should not be added $. The plus $ will empty the variable with the Array_Name value as the name.


Linux 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.