Linux Shell Programming (v): arrays

Source: Internet
Author: User
Tags array length

Array

An array is an ordered sequence of elements. If a collection of variables of the same type is named, the name is an array name. Each variable that makes up an array is called the component of an array, also known as an element of an array, sometimes called a subscript variable. The numeric number of each element used for the area fraction group is called the subscript. An array is a form of organizing a number of elements of the same type in an unordered form in the program design for ease of handling. These unordered collections of homogeneous data elements are called arrays.

There are two types of arrays in the shell:

    • Indexed array (indexed arrays)
    • Associative array (associative arrays)
Indexed array (indexed arrays)

Indexed arrays use numbers as subscripts, and subscripts start at 0 by default.

    • Declaring an array:
      [email protected]:~# # 直接声明[email protected]:~# array=(10 20 30 40 50 60 60 80 90) # 用空格分隔[email protected]:~# # 使用declare[email protected]:~# declare -a arrays # 声明一个索引数组
Associative array (associative arrays)

An associative array is an array that has a special indexed way. You can index it not only by integer, but also by using a string or other type of value (except null).

    • Array declaration
Array manipulation
    • accessing arrays

      [email protected]:~# echo ${array[0]} # array[index]10[email protected]:~#  # 使用通配符* 或 @ 可以访问数组中的所有元素[email protected]:~# echo ${array[@]}10 20 30 40 50 60 60 80 90[email protected]:~# echo ${array[*]}10 20 30 40 50 60 60 80 90[email protected]:~# array[0]=10 # 将下标为0的元素赋值为10
    • Array length
    • Get array subscript (key)
    • Array traversal

Use a For loop to iterate through an array

Linux Shell Programming (v): arrays

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.