bug-bash
[Top][All Lists]
Advanced

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

Assignment of associative arrays through braces


From: konsolebox
Subject: Assignment of associative arrays through braces
Date: Sat, 28 Jul 2018 07:13:41 +0800

Hi Chet,

I wonder if you can allow bash to have another syntax to allow simpler
declaration and/or definition of associative arrays.  The changes
needed to have it done seem simple enough, and the only conflict it
makes is a scalar `var={...` assignment, which in my opinion is better
quoted to make it more readable and less questionable from other
syntaxes like brace expansion.  I believe most people intuitively
quotes it, and assignments that start with `{` is fairly rare.

I made a patch for this which I include as an attachment.  I also
uploaded the source tree to
https://github.com/konsolebox/bash/tree/assoc_array_brace_assign.

Besides having an easy way to define associative arrays, this feature
would also allow associative arrays to be used as "return variables".
The caller wouldn't have to initialize the associative array unless it
wants to have it declared local for itself, and the callee that
returns the data can allow itself to be a complete and independent
function.

g() { a={[x]=y}; }
f() { local a={}; g; declare -p a; }
a={[a]=b}; f; declare -p a

Output:

declare -A a=([x]="y" )
declare -A a=([a]="b" )

-- 
konsolebox

Attachment: assoc_array_brace_assign.patch
Description: Text Data


reply via email to

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