Shell script programming Array _linux Shell

Source: Internet
Author: User
Tags arrays

An array is a set of elements of the same data type arranged in a certain order, that is, a variable of the same type is named with a name and then numbered to distinguish their set of variables, which is called an array name, and the number is called subscript. The variables that make up an array are called elements of an array. Arrays are in the program design, in order to deal with the convenience of the same type of a number of variables in order to organize a form.

One, the representation of the array

1, subscript array

Subscript must be an integer, format: array name [subscript], subscript default starting from 0.
The shell supports sparse arrays, and subscript integers do not need to be contiguous.
How to declare: DECLARE-A array name

2. Associative array

Associative arrays can use arbitrary strings as subscripts (not necessarily integers) to access array elements. Bash4.0 begins to support associative arrays.
Format: array name [arbitrary string]
Declaration by: declare-a Associative array
In addition, the shell supports only one-dimensional arrays and does not support multidimensional arrays.

Second, the assignment of the array


Three, array of access

Access to an array of elements is achieved by manipulating the syntax.

The syntax format has the following types:

Length of array:

${#ARRAY [*]}: Number of elements displayed;
${#ARRAY [@]}: Displays the number of elements;
${#ARRAY [#]}: Number of characters of the # element;
${#ARRAY}: Number of characters in the No. 0 element;

Elements of an array:

${array[*]}: Displays all element contents;
${array[@]}: Displays all element contents;
${array[#]}: Displays the # element;
${array}: Displays the No. 0 element;

The key value of the array:

$[! Array[*]}: Displays all the key values;
$[! array[@]}: Displays all the key values.

Example: Write a script that generates 10 random numbers, saves them to an array, and then displays the elements that are labeled even after the array:


Iv. Select elements from the array

If you want to take a few elements out of an array, it's more cumbersome to use the subscript to find each one, and you can use the Offset element feature of the array to simplify the operation.

Syntax format:

${array[@]:offset:number}
Offset: number of offsets;
Number: Amount of elements removed;

Instance: There are 6 elements in the array off_array, assuming we want to take the three values from March to May:


V. Array replication

$@: Each parameter is a separate string that is recommended for use;
$*: All elements as a string.

Instance: Copies an array of an even-numbered element into the new array:


Six, remove elements from the array

Format: unset array name [subscript]


Vii. Comprehensive Examples

Generates 10 random numbers and sorts the 10 random numbers.


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.