[clug] easier way to use IFS in bash?

Jeremy Kerr jk at ozlabs.org
Tue Aug 10 02:02:09 GMT 2004


Steve,

> Looking at using word splitting in bash, say I have a string and I want to
> return the 1st word before a whitespace character or the third word or
> something.

Arrays are your friend:

declare -a foo
foo=(zero one two)
echo ${foo[1]}

prints "one"

you can also specify a string to the () initialiser of the array:

somestring="zero one two"

declare -a foo
foo=($somestring)
echo ${foo[1]}



Jeremy


More information about the linux mailing list