Bash Scripting Arrays

Bash Scripting Using Arrays

Bash Arrays


Often referred to as elements. These elements are referenced by their reference number. This reference number must be a positive integer. The first number within an array is always "0" zero unless you specify a different number.

Values may be assigned in the following ways:

ARRAY[ElementNo]=value

Array variables may also be created using compound assignment:

ARRAY=(item1 item2 item3 item4 ....itemN)



Bash Array assignment examples



#!/bin/bash
#
# Array Example
#
test=(one two three four five)

echo ${test[0]}
echo ${test[1]}
echo ${test[2]}
echo ${test[3]}
echo ${test[4]}
echo ${test[*]}

Output from above array script



john@john-desktop:~/scripts$ ./array1.sh 
one
two
three
four
five
one two three four five

In the above example, our array variable "test" has been assigned five different values. These values are assigned an element number which starts at 0 through to 4. The corresponding echo statements reference each of these by element number. The last echo statement uses a "*" to display all elements within the specified array.

Additional elements can be added to an existing array simply: test[5]=six



#!/bin/bash
#
# Array Example
#
test=(one two three four five)

echo ${test[0]}
echo ${test[1]}
echo ${test[2]}
echo ${test[3]}
echo ${test[4]}

echo ${test[*]}

test[5]=six

echo ${test[*]}

Output from above array script



john@john-desktop:~/scripts$ ./array2.sh 
one
two
three
four
five
one two three four five
one two three four five six

In the above output, we can see that our additional entry has been added.



Using an array within our Odd or Even script


In one of our previous sections we used a script to determine if an entered number was odd or even. We could have rewritten this script to use an array:

Original Odd Even Script



#!/bin/bash
# Interactive Script for determining Odd or Even Numbers
#
read -p "Enter a number: " number
remainder=$(( $number % 2 ))

if [ $remainder -eq 0 ]; then
   echo "The number $number you entered is an even number"
else
   echo "The number you entered $number is an odd number"
fi

Odd even Script within an Array



#!/bin/bash
# Interactive Script for determining Odd or Even Numbers
#
test=(even odd) # test[0]='even' test[1]='odd'
read -p "Enter a number: " number
echo "The number entered is ${test[$((number%2))]}"

Output from above array script



john@john-desktop:~/scripts/shellscripts$ ./mod2.sh 
Enter a number: 40
The number entered is even
john@john-desktop:~/scripts/shellscripts$ ./mod2.sh 
Enter a number: 41
The number entered is odd

Deleting an Array using unset


To remove an array you can use the command unset. The unset command will destroy the array and its members:

Using the unset command against an array



john@john-desktop:~/scripts$ test[1]=land
john@john-desktop:~/scripts$ test[2]=of
john@john-desktop:~/scripts$ test[3]=linux
john@john-desktop:~/scripts$ echo ${test[*]}
land of linux
john@john-desktop:~/scripts$ unset test
john@john-desktop:~/scripts$ echo ${test[*]}


In the above example we assigned the values "land", "of" and "linux" to our array "test". We used the command echo ${test[*]} to display all elements within the array. Now after issuing the "unset" command against our array we can see that it is now empty.