Working with arrays

An array is a list of variables. For example, we can create an array called FRUIT, which will contain the names of many fruits. The array does not have a limit on how many variables it may contain. It can contain any type of data. The first element in an array will have the index value of 0:

[student@localhost ~]$ FRUITS=(Mango Banana Apple)
[student@localhost ~]$ echo ${FRUITS[*]}
Mango Banana Apple
[student@localhost ~]$ echo $FRUITS[*]
Mango[*]
[student@localhost ~]$ echo ${FRUITS[2]}
Apple
[student@localhost ~]$ FRUITS[3]=Orange
[student@localhost ~]$ echo ${FRUITS[*]}
Mango Banana Apple Orange

Creating an array and initializing it

You will now learn about creating an array in the Bash shell.

If the array name is FRUIT, then we can create an array, as follows:

FRUIT[index]=value

Index is the integer value. It should be 0 or any positive integer value.

We can also create an array, as follows:

$ declare -a array_name
$ declare -a arrayname=(value1 value2 value3)

This is an example:

$ declare -a fruit=('Mango' 'Banana' 'Apple' 'Orange' 'Papaya')
$ declare -a array_name=(word1 word2 word3 ...)
$ declare -a fruit=( Pears Apple Mango Banana Papaya )
$ echo ${fruit[0]}
Pears
$ echo ${fruit[1]}
Apple
$ echo "All the fruits are ${fruit[*]}"
    All the fruits are Pears Apple Mango Banana Papaya
$ echo "The number of elements in the array are ${#fruit[*]}"
    The number of elements in the array are 5
$ unset fruit
or
$ unset ${fruit[*]}

Accessing array values

Once we have initialized an array, we can access it, as follows:

${array_name[index]}

Create script array_01.sh, as follows:

array_01.sh

#!/bin/bash 
 
FRUIT[0]="Pear" 
FRUIT[1]="Apple" 
FRUIT[2]="Mango" 
FRUIT[3]="Banana" 
FRUIT[4]="Papaya" 
echo "First Index: ${FRUIT[0]}" 
echo "Second Index: ${FRUIT[1]}"

The output is as follows:

$ chmod +x array_01.sh
$./array_01.sh
First Index: Pear
Second Index: Apple

To display all the items from the array, use the following commands:

${FRUIT[*]}
${FRUIT[@]}

Create script array_02.sh, as follows:

array_02.sh

#!/bin/bash 
FRUIT[0]="Pear" 
FRUIT[1]="Apple" 
FRUIT[2]="Mango" 
FRUIT[3]="Banana" 
FRUIT[4]="Papaya" 
echo "Method One : ${FRUIT[*]}" 
echo "Method Two : ${FRUIT[@]}"

The output is as follows:

$ chmod +x array_02.sh
$./ array_02.sh
Method One : Pear Apple Mango Banana Papaya
Method Two : Pear Apple Mango Banana Papaya

Let’s see a few more examples:

$ city[4]=Tokyo

The fourth member of the array, city, is assigned the value Tokyo. Since it is the only element in the array, the array size will be 1.

$ echo ${city[*]}Tokyo

The size of the array city is 1, since any other member of the array is not yet initialized.

${city[*]} will display the only element of the array city:

$ echo ${city[0]}

city[0] has no value, and neither does city[1] and city[2].

$ echo ${city[4]}Tokyo

city[4] has the city name of Tokyo.

Assign the array countries, as follows:

$ countries=(USA  [3]=UK  [2]=Spain)

The array countries are assigned USA at index 0, UK at index 3, and Spain at index 2. We can observe here that it does not matter in which sequence we initialize the members of the array. They need not be given in a particular sequence.

The first element of the countries array is printed using the following:

$ echo ${countries[*]}
USA Spain UK
$ echo ${countries[0]}
USA

Identify the country at index 1., as follows:

$ echo ${countries[1]}
$ echo ${countries[*]}
USA Spain UK
$ echo ${countries[0]}
USA

Identify the country at index 1., as follows:

$ echo ${countries[1]}

There is nothing stored in countries [1].

Use the following to identify the country at index 2:

$ echo ${countries[2]}Spain

The third element of the countries array, countries [2], was assigned as Spain.

Use the following to identify the country at index 3:

$ echo ${countries[3]}
UK

The fourth element of the countries array, countries [3], was assigned as UK.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

5 + four =