bug-bash
[Top][All Lists]
Advanced

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

Re: declare a="$b" if $a previously set as array


From: Stephane Chazelas
Subject: Re: declare a="$b" if $a previously set as array
Date: Tue, 16 Dec 2014 13:02:28 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2014-12-16 12:30:52 +0000, Stephane Chazelas:
> 2014-12-16 01:53:52 -0600, Dan Douglas:
> [...]
> > That would be one way but I think this can be solved without going quite
> > so far. How do you feel about these rules?
> > 
> >     1. If a word that is an argument to declare is parsed as a valid
> >        assignment, then perform the assignment immediately as it appears
> >        lexically. If such a word is parsed as a simple assignment (with or
> >        without an index) then bash treats it as a simple scalar assignment 
> > to
> >        the variable or array element as any ordinary assignment would. If 
> > word
> >        is parsed as an assignment with array initializer list then bash 
> > treats
> >        it as such.
[...]
> Or are you saying that
> 
> declare -a/-A should be parsed differently from declare without
> -a/-A?
[...]

I beleive that's what you meant as that's the only way I can see
it make sense.

But then, what about:

type=-a
value='(foo bar)'
declare "$type" var=$value

You kind of need to expand the variables before you know what
type of parsing to do, which breaks it.

BTW, another difference with ksh:

$ b="a b" ksh -c '"typeset" a=$b; echo $a'
a b
$ b="a b" bash -c '"typeset" a=$b; echo $a'
a

It looks like typeset/declare is considered more as a keyword
in bash than it is in ksh.

I wonder how ksh does it.

$ b="a b" ksh -c '${cmd:-typeset} a=$b; echo $a'
a
$ cmd=typeset b="a b" ksh -c '"$cmd" a=$b; echo $a'
a
$ touch typeset
$ b="a b" ksh -c 'typeset* a=$b; echo $a'
a
$ b="a b" ksh -c 'command "typeset" a=$b; echo $a'
a b

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]