Subject: Re: Different behavior w/ and w/o local
To: Julio M. Merino Vidal <jmmv@menta.net>
From: David Laight <david@l8s.co.uk>
List: tech-userlevel
Date: 09/14/2003 11:17:45
On Sun, Sep 14, 2003 at 10:39:23AM +0200, Julio M. Merino Vidal wrote:
> Hi all,
> 
> consider the following two shell functions:
> 
> f1() {
> 	var=$1
> 	echo $var
> }
> 
> f2() {
> 	local var=$1
> 	echo $var
> }
> 
> And the following calls with their respective output:
> 
> $ f1 "foo bar"
> foo bar
> 
> $ f2 "foo bar"
> foo
> 
> Is this different behavior expected?  I've seen this happens with both /bin/sh
> and /bin/ksh, though there is no mention to this in manpages, afaict.
> Or should I be quoting assignments, like var="$1", to avoid problems?

It's always better to include variable expansions in "".....

Experiments show that 'bar' is made a local variable in f2.
So field splitting happens on the substition of $1.
This is to be expected since 'local' is syntactically a command, whereas
var=$1 is an assignment.

Not sure whether TOG says anything different...

	David

-- 
David Laight: david@l8s.co.uk