LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   passing array and variable to function in bash script (https://www.linuxquestions.org/questions/programming-9/passing-array-and-variable-to-function-in-bash-script-738458/)

ajaypitroda 07-07-2009 08:26 PM

passing array and variable to function in bash script
 
Hi Guys,

I need to pass an array variable and a string variable to function. So far I am able to pass array to function, but cant figure out how to pass along with array.
any help appreciated. thx in advance

osor 07-07-2009 09:06 PM

Why not something like this?
Code:

#!/bin/bash

foo()
{
        string=$1 && shift
        array=($@)
        echo "string is $string"
        echo "array is ${array[@]}"
}

array=(one two three)

foo bar ${array[@]}


ajaypitroda 07-07-2009 11:10 PM

Thx buddy, I spent last three days trying to figure this out.


All times are GMT -5. The time now is 11:29 AM.