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

In bash you can do

MYVAR="somevalue" ./myscript.sh 

and the variable MYVAR will be defined when myscript.sh is started.

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

 MYARR=( 1 2 ) ./myscript.sh MYARR[0]=1 MYARR[1]=2 ./myscript.sh declare -a MYARR=( 1 2 ) ./myscript.sh 
+4
source share
1 answer

Incredibly strange ... I have not seen this before.

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

 declare -a MYARR=( 1 2 ); . ./myscript.sh 
+4
source

Source: https://habr.com/ru/post/1495970/


All Articles