Re: Parameter expansion fails when assigning a local variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]


On Mon, 29 Jun 2009, Herbert Xu wrote:

> On Sun, Jun 28, 2009 at 11:39:16PM +0200, Cristian Ionescu-Idbohrn wrote:
> >
> > But wait a second.  Shouldn't quotes '"' around $@ protect the whole
> > $@? It does so in different other context.  Why not here?
>
> The whole point of $@ is to retain field splitting within double
> quotes:

Sure.  Quoted, like "$@".  I've no problem with that.

> > There's some parallel unnatural behaviour I noticed:
> >
> > 	local var=$(some forked thing here)
> >
> > $(...)
> > ^^   ^ doesn't seem to protect the resulting string. There seems to
> > be a difference between:
> >
> > 	local h=$(grep --help)
> >
> > and:
> >
> > 	local h="$(grep --help)"
> >
> > Can anything motivate that?  Is that POSIX too?
>
> As I said local is identical to any other utility in this respect,
> including shell functions.

Ok.  You're telling us how it's implemented in dash.  Other shells do it
different.  Are you implying dash is the only shell which does that thing
right?  Even though it is unnatural?

Let's try something else.  busybox ash and bash are doing the right thing.
dash and zsh got it wrong.  What do people think?

Please consider this example script:

---8<---
#!/bin/dash

set -e
#set -x

func() {
	local i=0 v xx="$@"

	for v; do
		i=$(($i + 1))
		echo "func arg. $i: '$v'"
	done
	echo "inside func: NOONE_SHOULD_TOUCH_THIS=$NOONE_SHOULD_TOUCH_THIS"
}

NOONE_SHOULD_TOUCH_THIS=protected
echo "main, bef. func: NOONE_SHOULD_TOUCH_THIS=$NOONE_SHOULD_TOUCH_THIS"
func "$@"
echo "main, aft. func: NOONE_SHOULD_TOUCH_THIS=$NOONE_SHOULD_TOUCH_THIS"

exit 0
---8<---

and run the script like this:

	$ <script path> abc NOONE_SHOULD_TOUCH_THIS=overwritten

Is it the intended behaviour?  Should one be able to overwrite script
function variables from command line?


Cheers,

-- 
Cristian

--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

Powered by Linux