I can not export an array from a bash script to another bash script like this:
export myArray[0]="Hello"
export myArray[1]="World"
When I write like this there are no problem:
export myArray=("Hello" "World")
For several reasons I need to initialize my array into multiple lines. Do you have any solution?
For arrays with values without spaces, I've been using a simple set of functions to iterate through each array element and concatenate the array:
The first function with turn the array into a string by adding the opening and closing parentheses and escaping all of the double quotation marks. The second function will strip the quotation marks and the parentheses and place them into a dummy array.
In order export the array, you would pass in all the elements of the original array:
At this point, the array has been exported into the value $arrayString. To import the array in the destination file, rename the array and do the opposite conversion:
The environment is just a collection of key-value pairs, both of which are character strings. A proper solution that works for any kind of array could either
These are covered in the other posts. If you know that your values never contain a certain character and your keys are consecutive integers, you can simply save the array as a delimited list:
And restore it in the child process:
I was editing a different post and made a mistake. Augh. Anyway, perhaps this might help? https://stackoverflow.com/a/11944320/1594168
Note that because the shell's array format is undocumented on bash or any other shell's side, it is very difficult to return a shell array in platform independent way. You would have to check the version, and also craft a simple script that concatinates all shell arrays into a file that other processes can resolve into.
However, if you know the name of the array you want to take back home then there is a way, while a bit dirty.
Lets say I have
So I want to take it home
We can see it being exported, by checking from a sub-process
Result :
If we absolutely must, use the env var to dump it.
Then
Before running the another script,
As lesmana reported, you cannot export arrays. So you have to serialize them before passing through the environment. This serialization useful other places too where only a string fits (su -c 'string', ssh host 'string'). The shortest code way to do this is to abuse 'getopt'
Use it like this:
This does not address unset/sparse arrays. You might be able to reduce the 2 'eval' calls in restore_array.
Jeez. I don't know why the other answers made this so complicated. Bash has nearly built-in support for this.
In the exporting script:
(Note, the double quotes are necessary for correct handling of whitespace within array elements.)
Upon entry to
importing_script.sh
, the value of themyArray
environment variable comprises these exact 26 bytes:Then the following will reconstitute the array:
CAUTION! Do not
eval
like this if you cannot trust the source of themyArray
environment variable. This trick exhibits the "Little Bobby Tables" vulnerability. Imagine if someone were to set the value ofmyArray
to) ; rm -rf / #
.Much thanks to @stéphane-chazelas who pointed out all the problems with my previous attempts, this now seems to work to serialise an array to stdout or into a variable.
This technique does not shell-parse the input (unlike
declare -a
/declare -p
) and so is safe against malicious insertion of metacharacters in the serialised text.Note: newlines are not escaped, because
read
deletes the\<newlines>
character pair, so-d ...
must instead be passed to read, and then unescaped newlines are preserved.All this is managed in the
unserialise
function.Two magic characters are used, the field separator and the record separator (so that multiple arrays can be serialized to the same stream).
These characters can be defined as
FS
andRS
but neither can be defined asnewline
character because an escaped newline is deleted byread
.The escape character must be
\
the backslash, as that is what is used byread
to avoid the character being recognized as anIFS
character.serialise
will serialise"$@"
to stdout,serialise_to
will serialise to the varable named in$1
and unserialise with:
or
e.g.
(without a trailing newline)
read it back:
or
Bash's
read
respects the escape character\
(unless you pass the -r flag) to remove special meaning of characters such as for input field separation or line delimiting.If you want to serialise an array instead of a mere argument list then just pass your array as the argument list:
You can use
unserialise
in a loop like you wouldread
because it is just a wrapped read - but remember that the stream is not newline separated: