键中有空格的关联数组 [英] Associative arrays with space in the key

查看:103
本文介绍了键中有空格的关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试通过以下方式在bash中创建一个关联数组

I am trying to create an associative array in bash in the following way

#!/bin/bash
hash["name"]='Ashwin'
echo ${hash["name"]}

这将在执行时打印所需的输出:Ashwin.

This prints the desired output : Ashwin when executed.

但是当键中有空格时,

#!/bin/bash
hash["first name"]='Ashwin'
echo ${hash["first name"]}

我收到以下错误

test2.sh: line 2: first name: syntax error in expression (error token is "name")

键中不允许有空格吗?

推荐答案

如果在值分配之前先使用declare -A hash,则脚本将按预期运行.

If you first use declare -A hash before value assignments, then the script runs as expected.

使用bash 4.2.25测试

Tested using bash 4.2.25

这篇关于键中有空格的关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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