bash arrays and associative arrays

Source: Internet
Author: User

1.1.1Array

arrays are variable arrays, multiple variables are grouped together and accessed by the same name. A number is a contiguous number of separate memory spaces (elements), each of which corresponds to a variable. Array elements are referenced by "array name [index]". An index is also called a subscript, starting with a number of 0 instead of 1 .

declaring an array: declare-a Array_Name

The array of bash supports sparse format, so-called sparse format is, only two elements in an array, but these two elements can be the first and nineth, the middle is empty.

1.1.1.1assigning values to array elements

(1) Assign only one element at a time

A_name[index]=value - Array name [index]= value

For example:

weekday[0]= "Sunday": Array Weekday 1th element is Sunday

Weekday[1]= "Monday":

(2) Assigning all elements at once

weekday= ("Sunday" "Monday" "Tuesday")

[Root tmp]# weekdays= (Sun Mon Tue Wed) [Root tmp]# echo ${weekdays[0]}sun[root tmp]# echo ${weekdays[1]}mon

(3) Specify index for assignment, support sparse format

weekdays= ([0]= "Sunday" [3]= "Thu" [6]= "Sat")

[Root tmp]# weekdays= ([6]=sun [0]=mon [1]=tue] [root tmp]# echo ${weekdays[2]} [root tmp]# echo ${weekdays[1]}tue[root tmp] # echo ${weekdays[6]}sun

(4) Read-a A_name, implements multiple data into the array.

Do not use - a becomes an element

[Root tmp]# read helloqw er t[root tmp]# echo ${hello[1]} [root tmp]# echo ${hello[0]}qw er t

Use-A will be separated by a space

[Root tmp]# read-a helloqw er t[root tmp]# echo ${hello[1]}er[root tmp]# echo ${HELLO[0]}QW
1.1.1.2referencing array elements

${array_name[index]}, curly braces are not to be omitted.

Get Array length (that is, get how many elements are in the array) : ${#array [*]}, ${#array [@]}

[Root tmp]# echo ${#hello [*]}3

When not marked, the default is the first element:

[Root tmp]# Echo $HELLOQW

Therefore, this is to display the number of characters in the first element:

[Root tmp]# echo ${#hello}2


Exercise: Write a script that generates 10 random numbers, saves them to a data set, and then displays the values of the elements with an even number of arrays indexed.

#!/bin/bash#for ((i=0;i<10;i++)); dorand[$i]= $RANDOMecho ${rand[$i]}done echo "========================" for I in ' SEQ 0 2 9 '; Doecho ${rand[$i]}done

Exercise: Write a script, defines an array whose elements are the names of all files in the/var/log directory that end with. Log, showing the number of rows per file.

#!/bin/bash#declare-a files files= (/var/log/*.log) for i in ' seq 0 $[${#files [*]}-1] '; Dowc-l ${files[$i]}done

1.1.1.3array slices

Pick one or some of the specified elements from the array: ${array[@]:offset:number}

Offset: The number of elements to offset.

Number: The count of the elements to be removed.

[Root ~]# weekdays= (Sun Mon Tue Wed Thu Fri Sat) [Root ~]# echo ${weekdays[@]:2:2}tue wed[root ~]# Echo ${weekdays[@]:3:2}w Ed Thu


All elements remaining after the offset is removed: ${array[@]:offset}

[Root ~]# echo ${weekdays[@]:3}wed Thu Fri Sat

All elements: ${array[@]}

1.1.1.4Array add element

array_name[${#array_name [*]}]= value

This addition of sparse formatting may cause overrides:

[Root ~]# hello= ([6]=QW [3]=er] [4]=ty] [root ~]# echo ${hello[@]}er ty qw[root ~]# hello[${#hello [*]}]=ui[root ~]# Echo ${h Ello[@]}ui Ty QW

The non-sparse format is completely fine:

[Root ~]# hello= (qw er ty UI) [root ~]# hello[${#hello [*]}]=nm[root ~]# echo ${hello[@]}qw er ty UI nm

1.1.1.5remove an element from an array

Unset Array[index], undo is not a reference, be sure not to add .

[Root ~]# weekdays= (Sun Mon Tue Wed Thu Fri Sat) [root ~]# unset weekdays[3][root ~]# echo ${weekdays[@]}sun Mon Tue Thu Fr I Sat
1.1.2Associative Arrays

Bash supports associative arrays from version 4.0, and array indexes can be custom strings. This means that the subscript of an array can no longer be a number.

define the method: declare-a Array_Name

[Root ~]# declare-a wdays[root ~]# wdays= ([Sun]=sunday [Mon]=monday] [tue]=tuesday] [root ~]# echo ${wdays[mon]}monday

Once you have an array, you can define more complex algorithms.

bash arrays and associative arrays

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.