Reading Bash variable strings using a for loop

Simple bash question - as usual.

Example code.

#!/bin/bash
bla=“groucho chico harpo zeppo”

for searchterm in $bla
do

done
exit

Question is:
In the above variable bla, how do I force the loop to read two terms separated by a space as one.
At the moment the loop will go through groucho to zeppo individually, so how do enter a variable such as “harpo zeppo” read as one item ?

I tried the manuals and all I can find and could not get a simple solution.
Ususually you would use “’“harpo zeppo”’” etc tricks but they dont work either.

So how would $bla look like below to read $searchterm in the loop as “groucho” “chico” "harpo " “zeppo” “harpo zeppo” ?

bla="groucho chico harpo zeppo <harpo zeppo>"

(something needs to replace “<” and “>” above to make “harpo zeppo” a single entity when read by the loop variable $searchterm )

Maybe try IFS as follows:

x="one two |three four"
IFS='|'
for i in $x; do echo $i; done

Now you get two groups separated by ‘|’

#!/bin/bash
x="one two three|four five"
IFS='|' 
# split into two groups by | # 
set -- $x
# $1 has first group
# $2 has second group

# now process first group separated by ' '
IFS=' '
for i in $1
do
  echo "$i"
done
# here is second group #
echo "$2"

Thanks a lot, that will sure work.
I now have to rewrite quite a large script with this…painful., but your suggestion sure works.
I was hoping for a solution that I could just group two terms together as is in my example, but I guess it cannot be done.

As usual, thanks a lot for your help.


Linux sysadmin blog - Linux/Unix Howtos and Tutorials - Linux bash shell scripting wiki