protecting whitespace from the Bourne "for" command

ROOT rodgers at maxwell.mmwb.ucsf.edu
Sat Dec 8 11:51:58 AEST 1990


Dear Netlanders,

Does anyone know how to protect whitespace in items to be passed to the
"for" operator of the Bourne shell?  Consider the script:

#! /bin/sh
#
# Define list
#
list="'a b' c"
#
# Use list
#
for item in $list
do
   grep $item inputfile
done
#
# Script complete

where "inputfile" might contain, for example:

a b
c
d

The idea is to grep for each of the regular expressions appearing in $list,
one at a time, in the file "inputfile".  In the above example,
"a b" is meant to comprise one such pattern, and "c" another.
I have tried all sorts of combinations of \, ', and " in the definition
of "list" and in the appearance of "$list" on the "for" command line,
in an attempt to prevent the shell from parsing arguments on the whitespace
contained within the the expr "a b", all to no avail.  One such combination
of failed quoting mechanisms is displayed above.

Please, no responses of the form "why do you want to do this," "use perl,"
"use awk," etc.  The above boils down the essence of a problem which appears
in quite a different context.

Any ideas????

Thanks and Cheerio, Rick Rodgers



More information about the Comp.unix.shell mailing list