Understanding Array Variables
An array allows you to collect a list into one variable. The easy way to create an array variable is to assign a value to one of its indices, like so:
name[index]=value
Here, name
is the name of the array, and index
is the position of the item in the array. (Note that index must be a number.) value
is the value that’s set for that individual item in the array.
The numbering system for arrays begins with 0. So, name[0]
would be the first item in the array. To create an indexed array, use declare
with the -a
option, like so:
[donnie@fedora ~]$ declare -a myarray
[donnie@fedora ~]$
Next, let’s create the list that will be inserted into the array, like so:
[donnie@fedora ~]$ myarray=(item1 item2 item3 )
[donnie@fedora ~]$
You can view the value of any individual item in the array, but there’s a special way to do it. Here’s what it looks like:
[donnie@fedora ~]$ echo ${myarray[0]}
item1
[donnie...