Shell script from getting started to complex Quad (array)

Source: Internet
Author: User

multiple values can be stored in an array. The Bash Shell supports only one-dimensional arrays (which do not support multidimensional arrays), and the subscript for array elements starts with 0.


The Shell array is represented by parentheses, and the elements are separated by a "space" symbol, with the following syntax:

Array_name= (value1 ... valuen)


Reading an array

# VI test.sh


#!/bin/bash

Array1= (a B c d)

echo "first element: ${array1[0]}"

echo "second element: ${array1[1]}"

echo "third element: ${array1[2]}"

echo "fourth element: ${array1[3]}"


Output:

# sh test.sh

First element: A

Second element: b

A third element: C

Fourth element: D


Gets all the elements in the array:

# VI test.sh


#!/bin/bash

Array1[0]=a

Array1[1]=b

Array1[2]=c

Array1[3]=d


echo "Elements of array: ${array1[*]}"

echo "Elements of array: ${array1[@]}"


Output:

# sh test.sh

Elements of an array: a B c D

Elements of an array: a B c D


Gets the number of elements in the array:

# VI test.sh


#!/bin/bash

Array1[0]=a

Array1[1]=b

Array1[2]=c

Array1[3]=d


echo "Number of elements of array: ${#array1 [*]}"

echo "Number of elements of array: ${#array1 [@]}"


Output:

# sh test.sh

Number of elements in array: 4

Number of elements in array: 4


Shell script from getting started to complex Quad (array)

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.