why does sh do this

Peter Gray pdg at cs.uow.edu.au
Tue Jun 4 17:40:41 AEST 1991


I have a question regarding sh and IFS.

>From my reading of the man page for sh the following
script should not work.

#!/bin/sh
IFS=":"; export IFS
echo fred
ls fred jim

My understanding is the shell should be looking for
commands "echo fred" and "ls fred jim". But it works fine.
On the ls command fred and jim are treated as 2 arguments.
On the other hand
the shell builtins seem to use the IFS as documented.

EG

echo "fred:jim jack:mary" | (IFS=":"; read a b c; echo "$a\n$b\$c")

produces

fred
jim jack
mary

The SUNOS man page for sh says

     After parameter and command  substitution,  the  results  of
     substitution  are scanned for internal field separator char-
     acters (those found in IFS) and split  into  distinct  argu-
     ments  where such characters are found.  Explicit null argu-
     ments ("" or '')  are  retained.   Implicit  null  arguments
     (those  resulting  from  parameters that have no values) are
     removed.

How does sh actually use IFS??? Why doesn't altering IFS alter
the interpretation of subsequent lines in a script?

pdg

Peter Gray                    Internet: pdg at draci.cs.uow.EDU.AU
Professional Officer          UUCP:     ...!munnari!draci.cs.uow.EDU.AU!pdg  
Dept of Computer Science      MHSnet:   pdg at draci.cs.uow.oz.au
University of Wollongong      Phone: +61 42 213770                       
N.S.W.  2500  Australia       Fax :  +61 42 213262                       



More information about the Comp.unix.shell mailing list