Shell Array Definition and acquisition

Source: Internet
Author: User
Tags array definition
Shell Array

Bash supports one-dimensional arrays (which do not support multidimensional arrays) and does not limit the size of arrays.

Similar to the C language, the subscript of an array element is numbered starting with 0. Gets the elements in the array to take advantage of subscript, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.

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:

数组名=(值1 值2 ... 值n)

For example:

array_name=(value0 value1 value2 value3)
Reading an array

The general format for reading array element values is:

${数组名[下标]}

For example:

valuen=${array_name[n]}
Gets the length of the array

The method of getting the length of the array is the same as getting the string length, for example:

# 取得数组元素的个数length=${#array_name[@]}# 或者length=${#array_name[*]}# 取得数组单个元素的长度lengthn=${#array_name[n]}

Shell Array Definition and acquisition

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.