Reputation:
given array arr
and string str
. If I send them to function in the next way:
func "${arr[@]}" ${str}
How can I define the array and the string in func
function?
function func {
local arr=....
local str=....
}
according to what I understood (It's correct?) if I will do: local arr=("$@")
I will get that:
arr[0]=the first argument - in this case it's will be all the array
arr[1]=str
(according to my checks local arr=("$@")
gives array exactly like arr (what that I sent) , apart of it's adds str
as a last element in this array..)
[I don't ask for "how to send array in bash", but, how can I choose the array from all the arguments]
Upvotes: 0
Views: 43
Reputation: 27205
$@
are all the parameters passed to the function.
Retrieving the last parameter is easy. Just use the last array entry.
Afterwards you have to delete the last entry from the array.
function func {
local arr=("$@")
local str="${arr[-1]}"
unset 'arr[-1]'
}
Make sure to always call func
with at least one parameter or the script will fail.
Upvotes: 1