help-bash
[Top][All Lists]
Advanced

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

Re: Is it possible to export a bash array variable?


From: Greg Wooledge
Subject: Re: Is it possible to export a bash array variable?
Date: Mon, 5 Jan 2026 11:37:54 -0500
User-agent: Mutt/1.10.1 (2018-07-13)

On Mon, Jan 05, 2026 at 09:26:02 -0700, Stan Marsh wrote:
> From: Zachary Santer
> >Haven't tested this, but how about:
> 
> >shopt -s lastpipe
> >printf '%s\0' "${foo[@]@k}" |
> >  gawk4 '
> >    BEGIN {
> >      RS = "\0"
> >    }
> >    {
> >      getline value
> >      foo[$0] = value
> >    }'
> 
> Yeah, that could work as well.  I've never used @k, so I have no idea
> what it does.  But it might prove useful to read up on it somewhere
> down the road.

hobbit:~$ unset ia aa
hobbit:~$ ia=(zero one two); declare -A aa=([foo]=bar [baz]=quux)
hobbit:~$ printf '<%s> ' "${ia[@]@k}"; echo
<0> <zero> <1> <one> <2> <two> 
hobbit:~$ printf '<%s> ' "${aa[@]@k}"; echo
<foo> <bar> <baz> <quux> 

printf '%s\0' "${array[@]@k}"   will therefore print a NUL-delimited
stream of alternating keys (indices) and values, for either type of array.
It's by far the safest and easiest way to serialize an array, if you're
transporting or storing the stream via something that can handle NUL
bytes.



reply via email to

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