Indexed arrays and associative arrays

Bash provides a feature to declare a list (or array) of variables in a one-dimensional array that can be an indexed array or associative array. The size of an array can be 0 or more.

Indexed arrays

An indexed array contains variables that may or may not have been initialized continuously. Indices of an indexed array start from 0. This means that the first element of an array will start at an index 0.

Array declaration and value assignment

An indexed array can be declared by just initializing any index as follows:

array_name[index]=value

Here, an index can be any positive integer or an expression must be evaluated to a positive integer.

Another way of declaring is by using the declare shell built in as follows:

declare -a array_name

We can also initialize an array with values during a declaration. Values are enclosed within parentheses and each value is separated with a blank space as follows:

declare -a array_name=(value1 value2 value3 …)

Operations on arrays

Initializing and declaring values to a variable is not sufficient. The actual usage of an array is when we perform different operations on it to get the desired result.

The following operations can be done on an indexed array:

  • Accessing an array element by an index: An element of an array can be accessed by referring to its index value:
    echo ${array_name[index]}
    
  • Printing the array's contents: The contents of an array can be printed if an index of an array is given as @ or *:
    echo ${array_name[*]}
    echo ${array_name[@]}
    
  • Obtaining the length of an array: The length of an array can be obtained using $# with the array variable:
    echo ${#array_name[@]}
    echo ${#array_name[*]}
    
  • Obtaining the length of an array element: The length of an array element can be obtained using $# on nth index:
    echo ${#array_name[n]}
    
  • Deleting an element or an entire array: An element can be removed from an array using the unset keyword:
    unset array_name[index]  # Removes value at index
    unset array_name  # Deletes entire array
    

The following shell script demonstrates the different operations on an indexed array:

#!/bin/bash
# Filename: indexed_array.sh
# Description: Demonstrating different operations on indexed array

#Declaring an array conutries and intializing it
declare -a countries=(India Japan Indonesia 'Sri Lanka' USA Canada)

# Printing Length and elements of countries array
echo "Length of array countries = ${#countries[@]}"
echo ${countries[@]}

# Deleting 2nd element of array
unset countries[1]
echo "Updated length and content of countries array"
echo "Length = ${#countries[@]}"
echo ${countries[@]}

# Adding two more countries to array
countries=("${countries[@]}" "Indonesia" "England")
echo "Updated length and content of countries array"
echo "Length = ${#countries[@]}"
echo ${countries[@]}

The output after executing this script is as follows:

Length of array countries = 6
India Japan Indonesia Sri Lanka USA Canada
Updated length and content of countries array
Length = 5
India Indonesia Sri Lanka USA Canada
Updated length and content of countries array
Length = 7
India Indonesia Sri Lanka USA Canada Indonesia England

The associative array

The associative array contains a list of elements in which each element has a key-value pair. The elements of an associative array are not referred by using an integer value 0 to N. It is referred by providing a key name that contains a corresponding value. Each key name should be unique.

The declaration and value assignment

The declaration of an associative array is done by using the -A option with the declare shell builtin as follows:

declare -A array_name

An associate array uses a key instead of an index within a square bracket in order to initialize a value as follows:

array_name[key]=value

Multiple values can be initialized in the following way:

array_name=([key1]=value1 [key2]=value2 ...)

Operations on arrays

A few operations on an associative array can be done similar to how an indexed array does, such as printing the length and content of an array. The operations are as follows:

  • Accessing an array element by the key name; to access an element of an associative array, use a unique key as follows:
    echo ${array_name[key]}
  • Printing associative array content: The following syntax is used to print an associative array:
    echo ${array_name[*]}
    echo ${array_name[@]}
    Obtaining the length of an array:
    echo ${#array_name[@]}
    echo ${#array_name[*]}
  • Getting the value and length of a given key:
    echo ${array_name[k]}  # Value of key k
    echo ${#array_name[k]}  # Length of value of key k
  • Adding a new element; to add a new element in an associative array, use the += operator as follows:
    array_name+=([key]=value)
  • Deleting an element of an associative array with the k key as follows:
    unset array_name[k]
  • Deleting an associative array array_name as follows:
    unset array_name

The following shell script demonstrates the different operations on an associative array:

#!/bin/bash
# Filename: associative_array.sh
# Description: Demonstrating different operations on associative array

# Declaring a new associative array
declare -A student

# Assigning different fields in student array
student=([name]=Foo [usn]=2D [subject]=maths [marks]=67)

# Printing length and content of array student
echo "Length of student array = ${#student[@]}"
echo ${student[@]}

# deleting element with key marks
unset student[marks]
echo "Updated array content:"
echo ${student[@]}

# Adding department in student array
student+=([department]=Electronics)
echo "Updated array content:"
echo ${student[@]}

The output after executing this script is as follows:

Length of student array = 4
Foo 67 maths 2D
Updated array content:
Foo maths 2D
Updated array content:
Foo maths Electronics 2D
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.110.5