Shell array: Definition of shell array, array length

Source: Internet
Author: User

The shell is much more programmatic in terms of programming than Windows batch processing, both in loops and operations.

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:
Array_name= (value1 ... valuen)
For example:

    1. Array_Name= (value0 value1 value2 value3)

Or

    1. Array_Name= (
    2. Value0
    3. Value1
    4. value2
    5. Value3
    6. )


You can also define individual components of an array individually:

    1. Array_Name[0]=value0
    2. Array_Name[1]=value1
    3. Array_Name[2]=value2

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

Reading an array

The general format for reading array element values is:
${array_name[index]}
For example:

    1. Valuen=${array_name[2]}

As an example:

    1. #!/bin/sh
    2. NAME [0]= "Zara"
    3. name[1 ]= "Qadir"
    4. name[2]=" Mahnaz "
    5. name[3]= "Ayan"
    6. name[ 4]= "Daisy"
    7. echo "first Index: ${name[0]}"
    8. echo "Second Index: ${name[1]}"

Run script, Output:

$./test.shfirst Index:zarasecond Index:qadir

Use @ or * to get all the elements in the array, for example:

    1. ${array_name[*]}
    2. ${array_name[@]}

As an example:

    1. #!/bin/sh
    2. NAME [0]= "Zara"
    3. name[1 ]= "Qadir"
    4. name[2]=" Mahnaz "
    5. name[3]= "Ayan"
    6. name[ 4]= "Daisy"
    7. echo "first Method: ${name[*]}"
    8. echo "Second Method: ${name[@]}"

Run script, Output:

$./test.shfirst Method:zara Qadir Mahnaz Ayan daisysecond Method:zara Qadir Mahnaz Ayan Daisy
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:

Plain text Copy
    1. # Gets the number of array elements
    2. Length=${#array_name [@]}
    3. # or
    4. Length=${#array_name [*]}
    5. # Gets the length of an array of individual elements
    6. Lengthn=${#array_name [n]}

Shell array: Definition of shell array, array length

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.