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 12:30:52 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

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.

Then, like in my proposal,

declare a='(1 2 3)'

and

declare -a a='(1 2 3}'

with valid scalar assignments would work like

a='(1 2 3)'

so like:

a[0]='(1 2 3)'

Or are you saying that

declare -a/-A should be parsed differently from declare without
-a/-A?

>       2. Any words that do not parse as valid assignments lexically during 
> step
>          1 undergo the usual set of expansions for simple commands and the
>          resulting words are saved.
> 
>       3. After evaluation of all words from steps 1-2, those that have 
> undergone
>          expansion per 2 are passed as arguments to declare. Declare then 
> parses
>          each of these arguments, testing for assignments that became valid as
>          a result of expansion. During this step, for assignments to variables
>          that have an array attribute set, or for all assignments if declare 
> has
>          been given the -a or -A option, declare will treat assignments that 
> have
>          initializer lists as array assignments accordingly. Otherwise, 
> declare
>          will treat all assignments as scalar assignments to a variable or 
> array
>          element. Words that still fail to parse as valid assignments are an
>          error.

If I understand correctly, does that mean:


declare 'a=($(uname >&2))'

(assuming "a" was previously declared as an array) should do the same
as:

eval 'a=($(uname >&2))'

?
 
> I think this covers all the bases and is very close to the current behavior
> with one exception. All of the problems Stephane and I have brought up deal
> with the situation where a word that parses lexically as a scalar assignment
> isn't treated as such, so that is the only required change. I don't think
> that change will break many scripts.
> 
> Some observations:
> 
>  * This solves the problem without breaking backwards compatibility and allows
>    declare -p output to continue to function.
[...]

Well, if "declare -p" continues to function which is not what I
understand your proposal to do, then:

a='($(uname>&2))' bash -c 's=$(declare -p a); a[0]=foo; eval "$s"'

would still run uname.

And:

a='($(uname>&2))' bash -c 'b[0]=; declare -l "b=$a"'

as well.

-- 
Stephane




reply via email to

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