On Sun, Mar 24, 2024 at 01:04:38PM -0400, Zachary Santer wrote: > On Fri, Mar 22, 2024 at 11:23 AM Chet Ramey <chet.ra...@case.edu> wrote: > > > > This is what you can do with @K. > > > > https://lists.gnu.org/archive/html/bug-bash/2021-08/msg00119.html > > > > Word splitting doesn't happen on the rhs of an assignment statement, so you > > use eval. The @K quoting is eval-safe. > > Yeah, but what can you do with @k?
You can write the keys and values to a stream/file. That's pretty much it -- but that's actually not too shabby. Imagine sharing a bash associative array with some other programming language and wanting to import it into that other language's dictionary/hash data structure. This is what you'd want. > The difference in expansion behavior between indexed and associative > array compound assignment statements doesn't make sense. As nice as it > is to have expansions that expand to eval-safe expressions, needing > eval less would be nicer. It would be pretty reasonable to have a builtin that could take an array name plus any number of additional argument pairs, and load those pairs as keys/values into said array. Then you could do something like this: declare -A hash=(...) kvlist=( "${hash[@]@k}" ) declare -A newhash addtoarray newhash "${kvlist[@]}" Some readers may observe that this looks a bit like Tcl's "array set" command. That's not a coincidence. Whenever I see "a list of alternating keys and values", that's where my mind goes. I'm debating mentally whether this hypothetical new builtin would only work with associative arrays, or also double as an "lappend" (append new elements to the end of a list/array) if given an indexed array name. I'm leaning toward having it be both. It wouldn't be any *more* confusing than the current situation already is. The main difference would be that with an indexed array, every argument after the array name becomes an array element, instead of half of them being keys and the other half being values. Then again, I'm not likely to attempt to implement it, so anyone who actually writes the code gets to make all the decisions.