why does sh do this

Eric Gisin eric at mks.mks.com
Tue Jun 4 13:41:36 AEST 1991


In article <1991Jun4.074041.5300 at cs.uow.edu.au> pdg at cs.uow.edu.au (Peter Gray) writes:
   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.

---

Right on the first page of sh(1) it says [a command is a list of
words separated by spaces or tabs]. When the script is
read, commands are split into a list of words. This is necessary
so the keywords, assignments, and regular words can be determined.
IFS is used for further splitting when the script is executed.

For example:
	cmd="ls fred jim"
	IFS=" "
	"$cmd"			# one word, "" prevents splitting
	$cmd			# three words, runs ls
	IFS=":"
	$cmd			# one word, does not run ls



More information about the Comp.unix.shell mailing list