Q:

Shell Programming: Access Element of List

It is my understanding that when writing a Unix shell program you can iterate through a string like a list with a for loop. Does this mean you can access elements of the string by their index as well?
For example:
foo="fruit vegetable bread"

How could I access the first word of this sentence? I've tried using brackets like the C-based languages to no avail, and solutions I've read online require regular expressions, which I would like to avoid for now.

A:

Pass $foo as argument to a function. Than you can use $1, $2 and so on to access the corresponding word in the function.
function try {
 echo $1
}

a="one two three"

try $a

EDIT: another better version is:
a="one two three"
b=( $a )
echo ${b[0]}

EDIT(2): have a look at this thread.

