Suspecting bug in /bin/sh's IFS

Oliver Fromme olli at
Wed Mar 18 05:13:53 PDT 2009


According to the sh(1) manpage and the SUSv3 wording, the
order of characters in the IFS variable should not matter
for field splitting.  However, I get strange results with
our /bin/sh ...

I'm trying to parse config files that look like this:

 ip   =
 host =
 desc = some description here

That is, the format of lines is this:

 <key> <whitespace>* "=" <whitespace>* <value>

Of course that should be simple:  Just add "=" to $IFS
and read the lines in a loop, like the following (the
echo command is for debugging; the real shell code uses
the setvar command instead):

 while read key val; do
         echo "'$key'  --  '$val'"
 done < config

However, the "=" characters and some spaces are included
in $val, which is wrong behaviour, I think.  I get this

 'ip'  --  '  ='
 'host'  --  '='
 'desc'  --  '= some description here'

If I change the IFS line like this (diff -u format):

 -    IFS="=$IFS"
 +    IFS="$IFS="

then I get the correct and expected output:

 'ip'  --  ''
 'host'  --  ''
 'desc'  --  'some description here'

Note that the characters in IFS are exactly the same, only
the order is different.

On Solaris, the same shell script produces correct output
in both cases, independent from the order of characters
in IFS.  I've also tested zsh and bash:  They also produce
correct output in both cases.

So this really seems to be a bug in FreeBSD's /bin/sh.

I'm about to file a PR, but I suppose it's better to ask
the standards mailinglist first, so here I am.

Best regards

Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing b. M.
Handelsregister: Registergericht Muenchen, HRA 74606,  Geschäftsfuehrung:
secnetix Verwaltungsgesellsch. mbH, Handelsregister: Registergericht Mün-
chen, HRB 125758,  Geschäftsführer: Maik Bachmann, Olaf Erb, Ralf Gebhart

FreeBSD-Dienstleistungen, -Produkte und mehr:

More information about the freebsd-standards mailing list