Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Exporting an array in bash script

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?

Answer*

Cancel
3
  • [Tue Apr 28 13:21:09 xxx@yyy ~]$ m=(abc def eke); cat /tmp/test.sh; ./tmp/test.sh #!/usr/bin/bash -f # m=() echo " debug +++++++++++ ${#m[@]}" for f in ${m[@]}; do echo "=======================> f = $f"; done if [[ ${#m[@]} -lt 1 ]]; then echo "error expect more than zeo" else for f in ${m[@]}; do echo "-----> f = $f"; done fi debug +++++++++++ 3 =======================> f = abc =======================> f = def =======================> f = eke -----> f = abc -----> f = def -----> f = eke [Tue Apr 28 13:26:29 xxx@yyy ~]$ Commented Apr 28, 2020 at 19:27
  • If this is all part of your answer, you should edit your answer and add it all there, properly formatted. Commented Apr 29, 2020 at 0:03
  • Did you try it? myArray will be unbound. Commented Mar 17, 2021 at 17:52