在密钥空间猛砸关联数组(Bash associative arrays with space in

2019-08-31 22:53发布

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

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

This prints the desired output : Ashwin when executed.

But when the key has a space in it,

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

i get the following error

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

Are keys not allowed to have spaces in it?

Answer 1:

如果你第一次使用declare -A hash值分配之前,则如预期运行该脚本。

使用bash 4.2.25进行测试



文章来源: Bash associative arrays with space in the key
标签: bash shell