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: Stan Marsh
Subject: Re: Is it possible to export a bash array variable?
Date: Sat, 03 Jan 2026 13:59:59 -0700

From:   Greg Wooledge
Subject: Re: Is it possible to export a bash array variable?
Date:   Sat, 3 Jan 2026 11:55:35 -0500

>On Sat, Jan 03, 2026 at 09:20:42 -0700, Stan Marsh wrote:
>> Is it possible?

>Not by default.  There's a compile-time option in config-top.h that
>you can enable:

>/* Define to 1 if you want to be able to export indexed arrays to processes
>   using the foo=([0]=one [1]=two) and so on */
>/* #define ARRAY_EXPORT 1 */

Interesting.  Thanks.  The one downside that I see is that if I change
this file (note that I have edited/changed this file for various
things in the past), then I think "make" will have to re-build
everything (since everything could potentially depend on the settings
in config-top.h).

I found this in variables.c:

--- Cut Here ---
#if ARRAY_EXPORT
#define BASHARRAY_PREFIX        "BASH_ARRAY_"
#define BASHARRAY_PREFLEN       11
#define BASHARRAY_SUFFIX        "%%"
#define BASHARRAY_SUFFLEN       2

#define BASHASSOC_PREFIX        "BASH_ASSOC_"
#define BASHASSOC_PREFLEN       11
#define BASHASSOC_SUFFIX        "%%"    /* needs to be the same as 
BASHARRAY_SUFFIX */
#define BASHASSOC_SUFFLEN       2
#endif
--- Cut Here ---

Which makes it sound like it would work for assoc arrays as well as
indexed.  Is that true?

Also, what exactly do you get in the exported variable?  Is it pretty
much the same as what you get from "declare -p array" ? (*)

(*)   foo=([0]=one [1]=two)
looks to me to be very similar to what "declare -p" produces.

Finally, note that my use case is I'd like an easy way to get the
contents of an assoc array into an external program (GAWK).  I've
developed a couple of kludgey ways to do it (**), but would like something
better.

(**) One of which is of the form: gawk '....' <(declare -p foo)

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

When responding to my posts, please try to refrain from giving bureaucratic 
answers.
If you have nothing useful to say, then just click Next and go on.



reply via email to

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