On Thu, Mar 18, 2021 at 02:15:19PM +0100, Alex fxmbsw7 Ratchev wrote:
> pseudo exmaple

Why not give a REAL example?

> declare -A big=( mess of data )
> var=$( declare -p big )
> 
> now to extract the elements, beware with mess of data i dont mean 'mess'
> 'of' 'data'

You don't write bash code to parse bash syntax.  You let bash do it
for you.

Apparently you are trying to serialize an associative array and transmit
it from one instance of bash to another.  That's totally fine, and
declare -p is the right tool for that.

In the second instance of bash, to reconstruct the serialized array,
you simply eval the serialization (or source it, if it's in a file).

In the first script:

declare -A hash=(...)
export serialized_hash="$(declare -p hash)"
otherscript

Then, inside otherscript:

eval "$serialized_hash"

Now you have an associative array named "hash", with a copy of the contents
from the first script.  If you want to "extract the elements", you simply
use "${hash[key1]}" and so on.

Reply via email to