Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Your bash code compiles and executes properly using Execute BASH Shell Script Online (GNU Bash, version 4.1.2). –  karel May 15 '13 at 1:26
    
@karel oh, it is not a script, these are commands to the command line, thx I clarified there.. –  Aquarius Power May 15 '13 at 1:36
add comment

1 Answer 1

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.

share|improve this answer
    
by safe you mean that a child shell cant update the array so its parent can make use of? I would like to use such array for ex. passing a list of selected file names (with spaces within the name), but I dont want a specific solution to that case as tomorrow I may want to pass a list of specific numbers or dates :). Also, btw, good you provided a tmp file as db because I remember I would like to avoid the creation of temporary files, as I may need to run the script simultaneously on different terminals. –  Aquarius Power May 16 '13 at 1:30
    
@AquariusPower not safe because there's no way to encode it so the child bash shell can determine that it's meant to be a bash array instead of a string. Keep in mind that environment variables are used by many programs, not just shells. E.g. man will behave differently depending on the value of a PAGER variable in the environment. –  geirha May 16 '13 at 10:10
    
what about tracking variable names into a string variable, and on each shell instance, run a function that use that control to re-interpret the array that was exported as string, back into a real array? I will do some tests and improve my question with this idea as soon I can. I wonder if such approach could be implemented on actual C bash code also? –  Aquarius Power May 16 '13 at 23:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.