Good grief so this should be easy. Passing an array as an argument to a function. Here is the sample code:
#/bin/bash
function foo {
local p1=${1}
local p2=(${2})
local p3=${3}
echo p1 is $p1
echo p2 is $p2
echo p3 is $p3
}
d1=data1
d2=data2
a=(bat bar baz)
foo "${d1}" "${a[@]}" "${d2}"
Obviously, it prints
data1
bat
bar
And what I want is:
data1
bat
data2
I would take
data1
bat bar baz
data2
As that would still be workable. I'm simply trying to stuff the array into the second argument somehow that isn't nasty and hard to understand. Obviously in the real example, there are multiple arrays being passed, so playing with the tokens isn't a great solution for me.
function foo {
local p1=${1};
local p2=(${2});
local p3=${3};
echo p1 is $p1;
echo p2 is $p2;
echo p3 is $p3;
}
d1=data1
d2=data2
a=(bat bar baz)
foo "${d1}" "${a[*]}" "${d2}"
p1 is data1
p2 is bat
p3 is data2