Reputation: 159
#!/bin/bash
myfunc() {
local new_arr
new_arr=("$@")
echo "Updated value is: ${new_arr[*]}"
}
my_arr=(4 5 6)
x="test1"
y="test2"
echo "Old array is ${my_arr[*]}"
myfunc ${my_arr[*]} $x $y
Output of the program is :
Old array is 4 5 6
Updated value is: 4 5 6 test1 test2
I want to access x,y and my_array separately inside the function myfunc(),
But I don't know the size of array in advance.
something like $1 would be my_array $2 would be x and so on..
But I Don't know how to do this in shell script.
Please note that my bash version is :- version 4.1.2
Upvotes: 1
Views: 73
Reputation: 765
#!/bin/bash
myfunc() {
local new_arr
first_variable=$1 && shift
second_variable=$2 && shift
new_arr=("$@")
echo "Updated value is: ${new_arr[*]}"
}
my_arr=(4 5 6)
x="test1"
y="test2"
echo "Old array is ${my_arr[*]}"
myfunc "$x" "$y" "${my_arr[@]}"
output Old array is 4 5 6
Updated value is: 4 5 6
Upvotes: 2