An array of bash

Source: Internet
Author: User

Bash 2.x provides the ability to create one-dimensional arrays.

  • There are several ways to create, using the built-in command declare-a or direct array elements to assign values.
  • When assigning a value to an array, if you do not specify a subscript, the subscript automatically starts at 0 and increments by 1 each time.
  • There is no limit to the size of the array, and the subscripts do not have to be numbers in a certain order.
  • The syntax for getting an array element is:${arrayname[index]}
  • The syntax for getting a list of all array elements is:${arrayname[*]}
  • The syntax for getting the number of array elements is:${#arrayname [*]}

$declare-A nums= (45 33 100 65)

$echo ${nums[0]}
45

$echo ${nums[*]}
45 33 100 65

$states = (ME [3]=CA [2]=CT]
$echo ${states[*]}
ME CA CT

$names = (Tom Peter Mike)

$names [5]=panda

$echo "All the array elements is ${names[*]}"
All the array elements is Tom Peter Mike Panda

$echo "The number of elements in the array is ${#names [*]}"
The number of elements in the array is 4

$unset names

=-=-=-=-=
Powered by Blogilo

An array of bash

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.