使用引号作为键击的HashMap [英] Bash hashmap using quote as key

查看:118
本文介绍了使用引号作为键击的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash中,我想取消设置一个HashMap的条目。但我失败了。 code是象下面这样:

In Bash, I want to unset an entry of a hashmap. But I failed. Code is like below:

declare -A arr
arr["a'b"]=3
echo ${!arr[@]} ## output: a'b
key="a'b"
unset arr[$key] ## error: -bash: unset: `arr[a'b]': not a valid identifier

我怎么可以不设置这个项目吗?

How can I unset this entry?

推荐答案

只需使用单引号:

$ declare -A arr=(["a'b"]=3 [foo]=bar)
$ declare -p arr
declare -A arr='(["a'\''b"]="3" [foo]="bar" )'
$ key="a'b"
$ unset 'arr[$key]'
$ declare -p arr
declare -A arr='([foo]="bar" )'

完成!

这篇关于使用引号作为键击的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆