Hey there!
OK, let's go through the code.
- ARRAY=("example program" "for test only" "Lets try");
This generates an array with the 3 elements "example program", "for test only" and "Lets try". Pretty easy.
OK, there are a few things in this line. First of all, if you have a variable name, that may not be recognised as one (for example when accessing an array like here), you can put braces around the name. So, if you wanted to access the first element of the array, you would use
${ARRAY[1]}, as just
$ARRAY[1] would resolve as
example program[1]. (Note: if you access an array without a couter, it will always return the first element.)
Next, the
[@] part. The brackets behind the name of an array mean, that you want to access a certain element of that array. Putting an @ symbol there means, that you want to access
all of the arrays elements. (Just like $@ means all of the arguments you gave, when calling the script.)
Last but not least, the hash symbol (#). The hash symbol is quite commonly used to express the amount of something - and that is how bash uses it here. So, #ARRAY[@] means "give me the amount of elements in ARRAY[@]" (which as I said before gives you all elements in ARRAY).
Gives you the number you just got.
- for((i=0;i<$ELEMENTS;i++));do
OK, for this you should know, that there are several forms of the for-loop in bash programming. Traditionally, you would use the form
which works with basically all forms of shell (e.g. sh, ksh, csh, ...). If you already know how many times you want to go through the loop, you can also use the form
, but that didn't work for me when I tested using
$ELEMENTS. In this case however, the programmer chose to use the C-like three-expression syntax. This form is easily understandable for anyone with a C-like background, but it's not very common in shell programming. Basically what happens is, that first a variable (i) is initialized, then there's a condition (i<$ELEMENTS) and then it is incremented (i++). The reason, that this whole thing has to be in double brackets is, that if you have single brackets, it's just a sequence of commands. For example,
will just output
0. Now, in pure bash syntax,
i<$ELEMENTS doesn't make much sense - to test if the value of
i is smaller than
$ELEMENTS, you would normally write
So, when this feature was introduced into bash syntax, they decided to use something they hadn't used before - double brackets.
Oh, and the
do starts a block of code, that the for loop should run.
- echo ${ARRAY[$i]} # echo ${ARRAY[${i}]}
The first echo will give you the i'th element of the Array for the reason I told you before. The second one won't do anything, as in this context the hash symbol starts a quote. The reason this quote was added is probably, because previously in the program, accessing variables was normally done with the braces and here he's not using them for the
i. So, to show you that it's just a different way of doing the same thing, the programmer added the comment.
This ends the block of code, which was started by
do earlier.
OK, hope this helped you. :-)
Greetings,
Nepomuk