Array as parameter

Can I pass and array as a function paramter?

#!/bin/bash

a1[1]=one
a1[2]=two
a2[1]=one
a2[2]=two

function f1()
{
   array1copy=( ${1[@]} )
   array2copy=( ${2[@]} )
   echo "${array1copy[@]}"
   echo "${array2copy[@]}"
}

f1 "${a1[@]}" "${a2[@]}"

I receive: ${1[@]}: bad substitution

Thank you

Try to pack the arguments into a single variable and then pass that variable to the function. Like this...

#!/bin/bash

argument1=`echo ${a1[@]}`

argument2=`echo ${a2[@]}`

f1 argument1 argument2

Vino

It is possible, but not without any effort:


function caller {
typeset array[1]="foo"
typeset array[2]="bar"

callee array
return 0
}

function callee {

typeset -i iCnt=0
(( iCnt = 1 ))
while [ $iCnt -le $(eval print \${#$1[*]}) ] ; do
    print - $(eval print \${$1[$iCnt]})
done

return 0
}

bakunin