Well, it is called the "Advanced" Bash Scripting guide for a reason. But it can be a good place to learn from all the same. The best study technique is simply to try out the examples yourself and see what happens. Then play around with them to learn more.
Or you could start with the
Bash Guide For Beginners and work your way up.
Anyway, an array is simply a variable with multiple entries, each one designated by an index number (or string, in bash 4's new associative arrays).
Let's start with a simple example.
Code:
#Set 3 array elements directly, just like regular variables. Array indexes start at 0.
MyArray[0]="Fry"
MyArray[1]="Bender"
MyArray[2]="Leela"
echo "${MyArray[2]} ${MyArray[1]} ${MyArray[0]}"
#output is: Leela Bender Fry
#The @ mark will give you all array elements at once.
echo "${MyArray[@]}"
#output is: Fry Bender Leela
There are a variety of ways to set an array, with the most common one being:
Code:
#Set a string to multiple array elements at once
MyArray=( This is a text string )
echo ${MyArray[0]} #output: This
echo ${MyArray[3]} #output: text
#Iterate through the array elements backwards
for i in 4 3 2 1 0; do
echo ${MyArray[$i]}
done
#output:
string
text
a
is
This
In the above setting, each word inside the parentheses becomes one array element, with word splitting done based on the IFS variable.
Hopefully that will be enough for you to understand how they work, and you can go back to ABS to learn the details.
I also recommend learning about
parameter substitution. The various forms there can also be used for trimming strings down to their useful parts. For example, you can use it as a replacement for basename.
Code:
path=/home/david/documents/myfile
filename=${path##*/}
echo $filename
#result: myfile
Most of the parameter substitutions can be used with array elements too, of course.