Consider I have a very large array $large_list
, is there a way to write a function that will take the array as an argument? For example:
echo_idx_array () {
arr="$1"
idx="$2"
echo "${arr[$idx]}"
}
What is the usual strategy to do something like that? I tried giving the variable $large_list
but it was empty.
I am willing to modify the function to adapt it to any change in the argument list.
For the record, I am using ksh88, and I am looking for answers as portable as can be.
EDIT: So far the best I could come up with is to loop through the array and send each element as an argument to the function. This seems incredibly ugly and error-prone, not to mention that it is bound to hit some limit quickly. Here's what I did:
foo () {
echo $*
}
cmd="foo "
while [[ $i -lt $MAX_ARR_SIZE ]]; do
cmd="$cmd ${large_list[$i]}"
((i=i+1))
done
eval $cmd
Isn't there something better to do?
func "${array[@]}"
? If you only need to pass one element, just pass the element - no need to make it more convoluted by passing an array and an index. – jw013 Jun 22 '12 at 12:14"${array[$@]}
. Your suggestion actually works. Mea culpa. – rahmu Jun 26 '12 at 15:11