Monday, December 29, 2008

Arrays in bash

Hello and welcome to this weeks installment of Practical shell scripting. This week I'm going to tackle something which is common in most coding languages, but it's handled a little differently in bash shell scripts, that is arrays. Hopefully most of you know that an arrays is a collection of like pieces of data. The data could be integers, or strings, or floats or whatever. For those of who come from a C/C++ background there are several things which are a little on the annoying side about using arrays in shell script. Firstly bash will allow you to mix data types that's because bash and I believe Bourne and Korn as well are what's referred to as loosely typed languages. So for instance in a language like C/C++ you define an arrays being of a specific data type. For example:

int MyNumbers[5]={1,3,5,7,11};

is a single dimensional array which will hold 5 integers, but if you try and introduce other variable types you will fail to compile. Bash on the other hand will you get away with a lot more. Also in Bash it's perfectly acceptable to declare your array without any predefined size, or variable type. So for instance in bash an array might look like this:

SomeAray="1 3 5 7 eleven";

However, the nice thing about bash arrays is that they follow a similar syntax regarding the syntax of the indexes, and like arrays in C/C++ they both start at 0. The only other tricky thing to remember is that in bash arrays are referenced using ${ }. As opposed to C/C++ where the array is just referenced directly. So to represent the fifth element of the C/C++ array you would refer to it as:

MyNumbers[4];

While in Bash you would reference the fifth element as:

${SomeArray[4]}

There are also a few nifty little things about bash arrays that I'm just starting to explore myself, but here's some code to play with to give you some ideas of what you can do with bash arrays.

#!/bin/bash

##-----------------------------------------------------------------##
##
## A simple shell script to give examples of arrays
##
##-----------------------------------------------------------------##


##--- example one ----##
echo "EXAMPLE 1: array of integers"
Array=(1 3 5 7 eleven)

echo "Here's the values"
for value in ${Array[*]}
do
echo "value is $value"
done


echo ""
echo "Here's the index"
for index in ${!Array[*]}
do
echo "index is $index"
done


echo ""
echo "Here's the index and it's corresponding value"
for index in ${!Array[*]}
do
echo "index is $index, value is ${Array[$index]}"
done


##--- example two ----##
echo ""
echo "So the value at index 2 is ${Array[2]}"
echo ""
echo "EXAMPLE 2: array of strings "
NewArray=(Apples Oranges Pears Beans 99)
echo "NewArray[0] is ${NewArray[0]}"
echo "NewArray[1] is ${NewArray[1]}"
echo "NewArray[2] is ${NewArray[2]}"
echo "NewArray[3] is ${NewArray[3]}"
echo "NewArray[4] is ${NewArray[4]}"



Notice how you can mix and match strings in with integers in the array and bash doesn't even care. There's a couple of other neat tricks that you can do with arrays, but I'll save that for next weeks entry.
That's it for this week, as always if you have any questions or comments please feel free to contact me and Happy coding.

No comments: