Site icon Technology and Trends

Arrays in Bash Scripts

An array is a data structure or a variable that can store multiple values. It does not have any limit on the size or any requirements that say members variables be indexed or assigned contiguously. In Bash, the index of the first element starts with the number 0.

When working with Arrays, we should be aware of the environmental variable called IFS or Input Field Separator. It is a character that separates elements in an Array, whose default value is an empty space IFS=''. Let’s look at the basic concepts of Array in Bash Script

Creating an Array

To declare a variable that can hold an array, we create it with declare command.

Its syntax is given below.

declare -a ArrayName

If we need to add missing or extra members to the array, we use the below syntax

ARRAYNAME[indexnumber]=value

Let’s take an example where we create an array using different ways.

drinks[0]=Fanta
drinks[1]=BlueMoon
drinks[2]=Stella

We can also create this array using the compound assignment.

drinks=(Fanta BlueMoon Stella)

Extracting the Array Elements

In order to refer to the Array items, we need to use curly braces. This is needed to bypass the interpretation of expansion operators.

Let’s look at a small example.

~$ declare -a sampArray=(1 3 6 8)
~$ echo ${sampArray[1]}
3
~$ echo ${sampArray[0]}
1
~$ echo ${sampArray[2]}
6

Declare command

Now, let’s understand the declare command

It is used to declare any variables and/or give them attributes.

Syntax:

 declare [-afFrxi] [-p] name[=value] ...

If we don’t give any name, it will display the value of the variables. Using -p option will display the attributes and value of each NAME.

There are other flags that can be used along with this command.

      -a        to make NAMEs arrays (if supported)
      -f        to select from among function names only
      -F        to display function names without definitions
      -r        to make NAMEs readonly
      -x        to make NAMEs export
      -i        to make NAMEs have the `integer' attribute set

When displaying values of variables, -f displays a function’s name and definition. The -F option restricts the display to function name only.

So here we see that the -a switch to declare makes the variable an array. So after getting that declare -a we declare the variable as an array, with the array within parentheses.

To make use of the array, we need to And then to make use of it, we use the way to write a variable like this: ${ variable_name} and the number inside the []’s is the number that points to which part of the array it should use, beginning from 0 which is the first.

Let’s make another short example:

#!/bin/bash
declare -a foo=(this is another example)
echo "The array (${foo[*]}) has (${foo[0]}) as first, and (${foo[3]}) as last."

The output of this would be like below.

The array (this is another example) has (this) as first, and (example) as last.

Now, this isn’t something you’ll use all the time. But it’s still something you should know the existence of, just in case you see it or need it at some point.

Adding Element to Array

Now we will assign a new value to the same array sampArray at an existing indent.

~$ sampArray[1]=s1
~$ echo ${sampArray[1]}
s1

Removing Element from Array or Deleting the Array Itself

To remove the element from the Array or delete/destroy the array itself, we can use the unset command.

Removing the Element from the Array

~$ declare -a sampArray=(1 3 6 8)
~$ unset sampArray[1]
~$ echo ${sampArray[1]}

~$ echo ${sampArray[*]}
1 6 8

Deleting the Array

We use the unset command to delete an element from an array.

~$ declare -a sampArray=(1 3 6 8)
~$ echo ${sampArray[*]}
1 3 6 8
~$ unset sampArray
~$ echo ${sampArray[*]}

Exit mobile version