How to pass array as function in shell script?
I written following code:
function test(){
param1 = $1
param2 = $2
for i in ${$param1[@]}
do
for j in ${param2[@]}
do
if($(i) = $(j) )
then
echo $(i)
echo $(j)
fi
done
done
}
but I am getting line 1: ${$(param1)[@]}: bad substitution
There are multiple problems:
=
when assigning variablestest
because that is a shell commandHere is the fixed version:
myFunction(){
param1=("${!1}")
param2=("${!2}")
for i in ${param1[@]}
do
for j in ${param2[@]}
do
if [ "${i}" == "${j}" ]
then
echo ${i}
echo ${j}
fi
done
done
}
a=(foo bar baz)
b=(foo bar qux)
myFunction a[@] b[@]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With