Simple bash question - as usual.
bla=“groucho chico harpo zeppo”
for searchterm in $bla
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"
for i in $x; do echo $i; done
Now you get two groups separated by ‘|’
x="one two three|four five"
# split into two groups by | #
set -- $x
# $1 has first group
# $2 has second group
# now process first group separated by ' '
for i in $1
# here is second group #
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.
bla="groucho chico harpo zeppo foo bar odd"
for i in $bla ;do
if [ "$x" = "" ] ;then
echo $x $i
The odd one at the end will be lost, but you didn’t
say anything about a case with an odd number of words.