Specs
GNU bash, version 3.1.17 (No possibility of upgrade)
Premise
I've been messing around with arrays and i was wondering if there is any way to have a variable local to a function with the same name as an array outside of said function.
Example
In the below examples I will try to display the issue
Working
#!/bin/bash
arr=(1 2 "3 4" 5) # Make global array
myfunc()
{
local args=("${!1}") # Using different name for declaration
echo ${args[@]} # Echo new array
}
myfunc arr[@] # Pass array to function
Output
1 2 3 4 5
Not working
#!/bin/bash
arr=(1 2 "3 4" 5) # Create array
myfunc()
{
local arr=("${!1}") #Not working
echo ${arr[@]} # Not working
}
myfunc arr[@] # Pass array to function
Output
[Blank]
Reason
I want to pass multiple arrays to the function but do not want to have a possible name collision with the array being passed in and the local array name.
Tried
As you can see above i have tried adding the local function. I've scanned the bash man page and cannot seem to find anything else that could provide the behaviour i desisre
Bash -x Results
+ arr=(1 2 "3 4" 5)
+ myfunc 'arr[@]'
+ arr=("${!1}")
+ local arr
+ echo
If any more information is needed then please let me know.
${arr[@]}in the function mean you are blowing up your spaces but that's a different issue.GNU bash, version 3.2.51and it works, any idea why it wouldn't on3.1.17?