我需要在Bash中从关联数组中删除所有键和值。使用的是GNU bash, version 4.4.20(1)-release (x86_64-pc-linux-gnu)
版本。不能仅仅使用unset
命令,因为我需要在一个函数内完成此操作。如果使用unset
命令并再次使用declare
声明,那么该数组将只存在于函数内部。
arrayFunction()
{
# Start the array Clearer
unset workingArray
declare -A workingArray
# End the array Clearer
workingArray[test]="bar"
echo "Inside the function: ${workingArray[test]}"
}
declare -A workingArray
workingArray[test]="foo"
echo "Before the function: ${workingArray[test]}"
arrayFunction
echo "After the function: ${workingArray[test]}"
输出:
Before the function: foo
Inside the function: bar
After the function:
输出的最后一行应该是bar
。
我要找的是一些放在函数内部的代码,以完全清空数组,同时保持该数组为全局变量。