bash函数中本地数组的大小不正确

时间:2013-01-22 04:22:22

标签: shell

foo()
{
    local arr=(-l largs\
               -m margs\
               -n nargs
              )
    echo "size of arr: ${#arr[@]}"
}
foo   # prints 1

以上代码打印1,但显然arr的大小应为6。我将代码更改为:

foo()
{
    arr=(-l largs\
         -m margs\
         -n nargs
        )
    echo "size of arr: ${#arr[@]}"
}
foo   # prints 6

后一版本删除了local,并打印出正确答案。所以为什么?当地有关系吗?

PS:GNU bash,版本3.00.15(1)-release(x86_64-redhat-linux-gnu)。

0 个答案:

没有答案