Re: No expansions performed while declaring an associative array using a list of keys and values

2020-12-14 Thread felix
On Fri, Dec 11, 2020 at 03:08:04PM +0300, Oğuz wrote:
> I was trying the new features of bash 5.1 and came across this inconsistent
> behavior:
> 
> $ foo='1 2'
> $ declare -A bar=($foo 3)
> $ declare -p bar
> declare -A bar=(["\$foo"]="3" )
> $
> $ bar+=($foo 3)
> $ declare -p bar
> declare -A bar=(["\$foo"]="3" ["1 2"]="3" )
> 
> Is there a particular reason to avoid performing expansions in `declare -A
> bar=($foo 3)'?

When declaring ``associative array'', I alway use ``quoted'' syntax: 

$ foo='1 2'
$ declare -A bar='($foo 3)'
$ declare -p bar
declare -A bar=(["1 2"]="3" )

Not sure how to explain this, but I'm not surprised seeing this working so.

$ bar+=($foo 3)
$ declare -p bar
declare -A bar=(["1 2"]="3" )

Seem ok for me!

-- 
 Félix Hauri  --  http://www.f-hauri.ch



Re: No expansions performed while declaring an associative array using a list of keys and values

2020-12-14 Thread Oğuz
On Mon, Dec 14, 2020 at 11:00 AM felix  wrote:

> On Fri, Dec 11, 2020 at 03:08:04PM +0300, Oğuz wrote:
> > I was trying the new features of bash 5.1 and came across this
> inconsistent
> > behavior:
> >
> > $ foo='1 2'
> > $ declare -A bar=($foo 3)
> > $ declare -p bar
> > declare -A bar=(["\$foo"]="3" )
> > $
> > $ bar+=($foo 3)
> > $ declare -p bar
> > declare -A bar=(["\$foo"]="3" ["1 2"]="3" )
> >
> > Is there a particular reason to avoid performing expansions in `declare
> -A
> > bar=($foo 3)'?
>
> When declaring ``associative array'', I alway use ``quoted'' syntax:
>
> $ foo='1 2'
> $ declare -A bar='($foo 3)'
> $ declare -p bar
> declare -A bar=(["1 2"]="3" )
>
> Not sure how to explain this, but I'm not surprised seeing this working so.
>
> $ bar+=($foo 3)
> $ declare -p bar
> declare -A bar=(["1 2"]="3" )
>
>
With that it got even more confusing, see:

$ foo='1 2'
$
$ declare -A X=($foo 3)
$ declare -p X
declare -A X=(["\$foo"]="3" )
$
$ X+=($foo 3)
$ declare -p X
declare -A X=(["1 2"]="3" ["\$foo"]="3" )
$
$ declare -A Y='($foo 3)'
$ declare -p Y
declare -A Y=(["1 2"]="3" )
$
$ Y+='($foo 3)'
$ declare -p Y
declare -A Y=([0]="(\$foo 3)" ["1 2"]="3" )

Where did the 0 come from?


> Seem ok for me!
>
>
I think it would be the best if the only difference between declaring a
variable using assignment builtins and bare assignment statements were that
those builtins had the ability to set variable attributes.


> --
>  Félix Hauri  --  http://www.f-hauri.ch
>
>


Re: No expansions performed while declaring an associative array using a list of keys and values

2020-12-14 Thread Chet Ramey

On 12/14/20 4:45 AM, Oğuz wrote:



 $ Y+='($foo 3)'
 $ declare -p Y
 declare -A Y=([0]="(\$foo 3)" ["1 2"]="3" )

Where did the 0 come from?


That's a scalar assignment. You quoted the parens so it can't be a compound
assignment. Since the variable is an array, and an assignment was performed
without a subscript, you get the default subscript of "0".

The difference between this and quoting the rhs of an assignment when you
use `declare' is that `declare' is a builtin, and so its arguments undergo
a round of expansion before `declare' sees them. That's the fundamental
difference between assignment statements and arguments to declaration
commands that look like assignment statements.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Request: a command to do a return completely in normal, ie. not subshell

2020-12-14 Thread Budi
Can we have a command to do a return completely (as if it returns from
main function) when it is being in a third nested function call, in
order to get back to shell prompt at once, in normal, ie. not
subshell?

My main function invocation here will fully shut down terminal if exit
command is given.

c(){
local p
#...

[ "$p" = err ] && kill $TID

#...
}

b(){
#...
c
#...
}

a(){
export TID=$$
#...

b
#...
#...
}