简体   繁体   中英

How to pass Array as a argument to shell script function along with other two string parameters?

#!/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

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM