Mowzer Mowzer - 1 year ago 49
Bash Question

How to pass array to bash shell script?

How do I pass an array as a variable from a first bash shell script to a second script.

AR=('foo' 'bar' 'baz' 'bat')
sh "$AR" # foo
sh "${AR[@]}" # foo

echo ${ARR[@]}

In both cases, the result is
. But the result I want is
foo bar baz bat

What am I doing wrong and how do I fix it?

Answer Source


sh "${AR[@]}"

which split the array elements in different arguments, i.e

sh "${A[0]}" "${A[1]}" "${A[2]}" ...

and in use


to collect the command line arguments into an array.