Definition and use of Shell arrays

Source: Internet
Author: User

Defining arrays

In the shell, the array is represented by parentheses, and the elements of the array are separated by a "space" symbol. The general form of the definition array is:
Array_name= (value1 ... valuen)
For example:

Array_name= (value0 value1 value2 value3)
Or
Array_name= (
Value0
Value1
value2
Value3
)
You can also define individual components of an array individually:
Array_name[0]=value0
Array_name[1]=value1
Array_name[2]=value2 can not use successive subscripts, and there is no limit to the range of subscripts. Read array The general format for reading array element values is:
${array_name[index]}
For example:
Copy Plain Text New window
VALUEN=${ARRAY_NAME[2]} Use @ or * to get all the elements in the array, for example:
${array_name[*]}
${array_name[@]} Gets the length of the arraythe method of getting the length of the array is the same as getting the string length, for example:
# 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]} string conversion arraystr= "ABC SDG gas GER GT Ads"arr= ($STR)
Array traversalstr= "ABC SDG gas GER GT Ads"For s in ${str[@]}; DoEcho $s Done

Definition and use of Shell arrays

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.