默认情况下,关联数组在函数体内声明为本地,它们应该是全局的。以下代码
#!/bin/bash f() { declare -A map map[x]=a map[y]=b } f echo x: ${map[x]} y: ${map[y]}
产生输出:
x: y:
而这个
#!/bin/bash declare -A map f() { map[x]=a map[y]=b } f echo x: ${map[x]} y: ${map[y]}
产生输出:
x: a y: b
是否可以在函数内声明全局关联数组?
还是可以使用什么工作?
From: Greg Wooledge
Sent: Tue,23 Aug 2011 06:53:27 -0700
Subject: Re: YAQAGV (Yet Another Question About Global Variables)bash 4.2 adds “declare -g” to create global variables from within a
function.
谢谢格雷格但是Debian Squeeze仍然有Bash 4.1.5