Re: Is it possible to export a bash array variable?
From:
Zachary Santer
Subject:
Re: Is it possible to export a bash array variable?
Date:
Mon, 5 Jan 2026 10:59:12 -0500
Haven't tested this, but how about:
shopt -s lastpipe
printf '%s\0' "${foo[@]@k}" |
gawk4 '
BEGIN {
RS = "\0"
}
{
getline value
foo[$0] = value
}'
On Mon, Jan 5, 2026 at 10:05 AM Stan Marsh <gazelle@xmission.com> wrote:
>
> This seems to handle all the weird cases and is easier than parsing "declare
> -p".
> Although not shown above, one of the cases in my test data had newlines in
> the data value,
> and that is handled by the above code, which converts them to explicit "\n"s.
> I suppose I
> should also do that for the indexes as well.
Null-terminating your data and then reading it like that is going to
be a lot simpler than trying to escape newlines. What if there's
something in your data that already looks like an escaped newline?
etc.
Set ORS = "\0" in your BEGIN block if you want to also print
null-terminated data from awk. This could be read in bash with
read -d ''
or
mapfile -d ''