Linux How To's | Bash Scripting | Python

Friday, 11 October 2013

We have been dealing with some simple Bash Scripts in our recent articles on Basic Linux Shell Scripting Language. The variables we used in those scripts are called as 'Scalar Variables' as they can hold only a single value. Linux shell provides an another kind of variable which stores multiple values, either of a same type or different types, known as 'Array Variable'. Rather than creating a separate variable for each value, Array variable lets the programer to use a single variable to store multiple values at the same time. There is no maximum limit for the Array Size and no such compulsion to store the values in contiguous manner.

This article explains how arrays are defined in the Bash scripts, how they are used in the Bash scripts and some basic operations on them. Those who are new to Linux Shell Scripting are recommended to read our article - Getting Started - Basic Linux Shell Scripting Language.

Initializing the Arrays

Let us compare the Scalar variables and Array variables on the basis of their declaration.

The method of initializing Scalar variables is pretty straight forward. You just need a variable, a value and a '=' operator to assign that value to the variable.

Syntax:

varName=Value

Example:

myName="Mandar"
myAge=24

As quoted above, we can use a single Array variable to store multiple values. In the simplest method for storing a value in any Array variable we need name of the array and an index. If you are familiar with C Language (or any other programming language I should say), you will be aware of following Array initialization syntax.

Syntax:

arrayName[index]=Value

Note: First element of any array will be it index '0', i.e. arrayName[0] will be the first element of the arrayName.

Note: The braces are required to avoid conflicts with pathname expansion.

Displaying the Entire Array

You can access all the items at once using any one of the ways mentioned below:

Syntax:

${arrName[@]}
OR
${arrName[*]}

Example:

${Fruits[@]}
OR
${Fruits[*]}

Let us verify this by running the following script:

#!/bin/bash
Fruits=(Apple Mango Orange Banana)
echo "${Fruits[@]}"

Result:

Apple Mango Orange Banana

Displaying the Length of an Array and Array Elements

We can display the length of the whole array or any array element by using a special operator '#'. Here, length of an array will be displayed in terms of number of elements present in it whereas length of an array element will be in terms of number of characters in that element.

Removing an Element From an Array

To remove an element completely from an array completely, we need to merge two sub-arrays: First sub-array will consist of all those elements present before the element to be removed and Second sub-array will contain all those elements arriving after the element to be removed. Let us consider that, we have to remove 'Banana' present at index 3 in 'Fruits' array.

In above script, ${Fruits[@]:0:$idx} fetches 3 elements starting from index 0, i.e. Fruits[0], Fruits[1] and Fruits[2], and ${Fruits[@]:$(expr $idx + 1)} extracts all the elements from index 4 to the last index. These two sub-arrays are merged to create a new 'Fruits' array which does not contain 'Banana'

Result:

Apple Mango Orange Grapes Watermelon Blackberry Blueberry

Another alternative to remove an element from an array is to assign a 'null' value to the element. You can assign a null value to an element using 'unset' as follows:

The difference in both these techniques to remove an element from an array will be observed when you add following line in the script:

echo "${Fruits[3]}"

If you use first method to remove the element from the array, above line will show you 'Grapes' in the result. If you use 'unset', then it will display null value which was assigned to the concerned element.

You can also remove an array element using patterns as shown in following script: