Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH: how to define an array as environment variable before calling a bash script

Tags:

arrays

bash

shell

In bash, you can do

MYVAR="somevalue" ./myscript.sh

and the variable MYVAR will be defined when running myscript.sh.

My questions is: can I do the same for arrays? Unfortunately, neither of the following works.

MYARR=( 1 2 ) ./myscript.sh
MYARR[0]=1 MYARR[1]=2 ./myscript.sh
declare -a MYARR=( 1 2 ) ./myscript.sh
like image 568
Konstantin Avatar asked Aug 08 '13 14:08

Konstantin


1 Answers

Incredibility weird.... I have never seen that before.

It looks like the array is not passed to the subshell. One way around this is to source the script instead of executing it:

declare -a MYARR=( 1 2 ); . ./myscript.sh
like image 143
dtorgo Avatar answered Sep 23 '22 13:09

dtorgo