protecting whitespace from the Bourne "for" command

Martin Weitzel martin at mwtech.UUCP
Tue Dec 11 23:20:36 AEST 1990


In article <16570 at cgl.ucsf.EDU> rodgers at maxwell.mmwb.ucsf.edu (ROOT) writes:
>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

If you have any character that will never appear in the items of your
list, you can use this character as delimiter for the items and change
IFS (in most cases it is wise to restore IFS for the rest of the script):

	list="a b:c"
	CIFS=$IFS			# save IFS
	IFS=:
	for item in $list
	do
		IFS=$CIFS		# restore IFS here for the loop
		grep "$item" inputfile
	done
	IFS=$CIFS	# or restore it here for the rest of the script


In my example I used `:' as delimiter character; if you need all the
printing characters you can use some control character (e.g. BEL,
aka ^G), and, if you do it right, you can even use a newline character:

	list="a b
	c"
	IFS="
	"  # ^-- no white space between double quote and newline!!!
	for item in $list
	do
		grep $item inputfile
	done

In this example I left out saving and restoring IFS. 

Now, as we just touched this topic (and for all who don't know):
IFS contains the characters that are used as separators for the
command name and its parameters. In the times I had less experience
with the (Bourne-) shell, I thought the above (second example)
couldn't work, because: How does the shell separate the parts of
the command-line in the body of the loop, when no blank (space character)
occurs within IFS?

The answer is that the space character is *allways* a valid separator,
no matter what is specified in IFS. So the line

	grep $item inputfile

is correctly tokenized into three parts. Then, after several other
shell constructs were recognized, there is a step which replaces the
construct `$item' by he contents ov the variable `item'. And finally,
there comes the step where IFS is obbeyed and the line is further
separated.

For this reason you need no doublequotes around `$item' in the second
example, because IFS doesn't contain a space then, but you absolutely
need them in the first example!! (Think about it, if this is not clear
to you - and then try it.)
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.unix.shell mailing list