Passing an array into a collision of function names

the functions

GNU bash version 3.1.17 (non-upgradeable)


Premise

I worked with arrays, and I was wondering if there is a way to have a local variable for a function with the same name as the array outside the specified function.


Example

In the examples below, I will try to display the problem

IN

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

Exit

 1 2 3 4 5 

Does not work

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

Exit

 [Blank] 

Cause

I want to pass multiple arrays to a function, but I don’t want to have a possible collision of names with the passed array and the name of the local array.


I tried

As you can see above, I tried to add a local function. I looked through the bash man page and cannot find anything that could provide the behavior I desisre

Bash -x Results

 + arr=(1 2 "3 4" 5) + myfunc 'arr[@]' + arr=("${!1}") + local arr + echo 

If you need more information, please let me know.

+4
source share
1 answer

Congratulations, you are experiencing a bug in the 3.1 bash series.

From the Bash ChangeLog in the section related to the release of bash -3.2-alpha:

This document describes the changes between this version, bash -3.2-alpha, and the previous version, bash -3.1-release.

...

f. Two errors with creating a local array variable when shading a variable with the same name from the previous context are fixed.

+2
source

All Articles