bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: passing arrays as parameters to functions


From: Chris F.A. Johnson
Subject: Re: passing arrays as parameters to functions
Date: Sat, 18 Apr 2009 15:02:53 -0400 (EDT)
User-agent: Alpine 1.00 (LRH 882 2007-12-20)

On Fri, 17 Apr 2009, sharkura wrote:


I am looking for a way to pass an array variable as an argument to a
function.  The only remotely similar post I found dealt with spaces in
arguments, which is not my problem.

I want to pass three arguments to a function.  I want to receive them as
three positional parameters, $1 (an array), $2 (a scalar integer), and $3
(another scalar integer).  The integers are optional.

Currently, if I pass my array argument as ${arrayArg[@]}, the contents
actually expand to positional parameters $1, $2, ... $N.  I could just
assume that the last two positional parameters are my two integer arguments,
but I'd like them to be optional.  The size of the array is not fixed.

func()
{
  local f_array
  eval "f_array=( \"\${$1[@]}\" )"

  printf "%s\n" "${f_array[@]}"
}

a=( qw er ty ui op )
func a

--
   Chris F.A. Johnson, webmaster         <http://woodbine-gerrard.com>
   ===================================================================
   Author:
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)




reply via email to

[Prev in Thread] Current Thread [Next in Thread]