Array of Shell programming

Source: Internet
Author: User

Bash programming only supports one-dimensional arrays, does not support multidimensional, like C, array subscript starting from 0, subscript can be an integer, or it can be an expression

Definition of an array

Use parentheses in the shell to represent the array, separated by a space in the middle

There are two main forms of definition:

Arr= (1 2 3 4 5 6)

Arr= (

1

2

3

4

You can also define the components of an array separately

Arr[0]=1

arr[1]=2

Arr[2]=3

Arr[3]=4

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

Reading an array

The general format of reading data is ${arr[index]}

echo ${arr[1]} >>2

Use @ or * to get all the elements in the array

${arr[*]} >>1 2 3 4

Gets the length of the array

Gets the length of the array and gets the string length in the same way as echo ${#arr [*]} echo ${#arr [@]}

Gets the length of an array of individual elements echo ${#arr [n]}

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.