Shell array Introduction and related operations

Source: Internet
Author: User
Tags array length

Introduction

Working on the Linux platform, we often need to use the shell to write some useful and meaningful scripting programs. Sometimes, the shell array is used frequently. So, how does the array in the shell behave, and how is it defined? The next step is to explain the array in the shell.

Definition of an array

What is an array? Students who have studied computer programming languages know that the attributes of an array are a set of identical data types (not including the concept of associative arrays proposed by some programming languages). So what is the definition of arrays in the shell, we look at two types of data: One is a numeric type, the other is a string type, and although the shell itself is a weak type, it can be so distinguished.

  array of numeric types: A pair of parentheses represents an array, and the elements in the array are separated by a "space".

As an example:

Arr_number= (1 2 3 4 5);

  array of string types: Similarly, use a pair of parentheses to represent the array, where the elements in the array are enclosed in double or single quotation marks, and also with "spaces."

arr_string= ("abc" "EDF" "SSS"); or arr_string= (' abc ' EDF ' SSS ');

Operation of the array

We use the numeric type array arr_number= (1 2 3 4 5) as the source array for the relevant explanation: Get the array length, read the value of a subscript, assign a value to a subscript, delete, assign and replace, and traverse. To raise a point of knowledge, we want to get the value of a variable in the shell, starting with the $ character, such as: $a or ${a}.

  Get array length

arr_length=${#arr_number [*]} or ${#arr_number [@]}, i.e. form: ${#数组名 [@/*]} to get the length of the array.

  Reads a value of a subscript

ARR_INDEX2=${ARR_NUMBER[2]}, i.e. form: ${array name [subscript]}

  Assign a value to a subscript

Here are two questions to ask:

    The first question is , what happens if the subscript element already exists?

    A : the underlying value is modified to be the new specified value.

For example: arr_number[2]=100, the array is modified to (1 2 100 4 5)

    The second question is , if the specified subscript already exceeds the size of the current array, such as the size of the arr_number above is 5, specify an subscript of 10 or 11 or greater than 5 of any value?

    A : the newly assigned value is appended to the end of the array.

For example: arr_number[13]=13, the array is modified to (1 2 100 4 5 13)

  Delete operation

Clear an element: unset arr_number[1], this clears the array labeled 1;

Empties the entire array: unset arr_number;

  Shard Access

The Shard Access form is: ${array name [@ or *]: start subscript: End subscript}, note that the value of the end subscript element is not included.

For example: ${arr_number[@]:1:4}, where Shard Access starts with subscript 1 and the number of elements is 4.

  Mode substitution

Form: ${array name [@ or *]/mode/new Value}

For example: ${arr_number[@]/2/98}

  Traversal of an array

Array traversal We use the FOR statement to demonstrate:

${arr_number[@]} represents the entire array.

For V in ${arr_number[@]}; Do

Echo $v;

Done

Shell array Introduction and related operations

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.