通过阵列的功能名称冲突 [英] Pass array to function name collision
问题描述
GNU bash中,3.1.17版(升级没有可能)
GNU bash, version 3.1.17 (No possibility of upgrade)
我一直插科打诨与阵列,我想知道是否有办法有一个局部变量的函数具有相同的名称作为外部阵列功能说。
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.
在下面的例子,我将尽力显示问题
In the below examples I will try to display the issue
工作
#!/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
输出
1 2 3 4 5
不工作
#!/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
输出
[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.
正如你可以看到上面我已经尝试添加本地函数。我已经扫描bash的手册页和似乎无法找到任何东西,可以提供我desisre行为
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
的击-x结果的
+ arr=(1 2 "3 4" 5)
+ myfunc 'arr[@]'
+ arr=("${!1}")
+ local arr
+ echo
如果需要更多的信息,那么请让我知道。
If any more information is needed then please let me know.
推荐答案
恭喜您已经打一个bug在3.1系列的bash的。
Congratulations you've hit a bug in the 3.1 series of bash.
从猛砸更新日志在该条中关于与bash 3.2- alpha版本:
From the Bash ChangeLog in the section relating to the bash-3.2-alpha release:
本文档详细介绍这个版本时,bash-3.2-α之间的变化,
和previous版本的bash-3.1版本。
This document details the changes between this version, bash-3.2-alpha, and the previous version, bash-3.1-release.
...
F。遮蔽变量时固定两个错误与当地数组变量创建
从A previous背景相同的名称。
f. Fixed two bugs with local array variable creation when shadowing a variable of the same name from a previous context.
这篇关于通过阵列的功能名称冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!