Celeb Glow
news | March 24, 2026

how to export bash arrays? any workaround tips?

I am using the bash version 4.2.37 at ubuntu 12.10. But since ubuntu 12.04 I am unable to export arrays in bash...

This is an example (type these lines in the command line, it is not a script):

export astr=(a "b c" d)
declare -p |grep astr
bash
echo ${astr[@]}
declare -p |grep astr

echo outputs nothing...

declare -p |grep astr outputs nothing either...

What I am looking for is an workaround, because as far I know that is considered an known bash bug.

EDIT: btw, if possible, the workaround could avoid creating temporary storage files for the array as I may run the same script simultaneously on different shells.

2

2 Answers

It's not a bug with bash, there's just not any safe way to put a bash array in the environment.

As for workarounds, that depends on what you're trying to achieve with exporting arrays in the first place.

One possible workaround, for some cases, is to dump the array to a file and source that file where you need it.

astr=(a "b c" d)
declare -p astr > some_file
bash -c 'source ./some_file; printf "%s\n" "${astr[1]}"'

Another workaround could be to pass the array on as arguments to the next shell.

astr=(a "b c" d)
bash -c 'astr=("$@"); printf "%s\n" "${astr[1]}"' _ "${astr[@]}"
5

Wait wait wait... I know that I'm a little late here, but this question is still relevant when we are looking for exporting arrays on google so, my workaround was:

declare -a ASTR;
export ASTR=(`echo a b c`);
# Printing all elements content
echo "${ASTR[*]}";
# Printing a position
echo "${ASTR[2]}";

It will return c once arrays first position is 0. I hope that helps someone looking for it.

Regards

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy