bash关联字典不起作用(声明:-A:无效选项)

时间:2017-04-06 20:46:30

标签: bash macos dictionary

我已经关注了每个bash字典示例,但没有一个能够正常工作。我现在有

_CONFIG:

#!/bin/bash


instance_map["dev"]=project-dev
instance_map+=( ["stage"]="project-staging" )

declare -A animals=( ["moo"]="cow" ["woof"]="dog")

test.sh:

#!/bin/bash

source _CONFIG


echo ${instance_map["dev"]}
echo ${instance_map["stage"]}


echo "${animals[moo]}"
for sound in "${!animals[@]}"; do echo "$sound - ${animals[$sound]}"; done

出于某种原因,在按照每个例子后,我得到了

$ ./test.sh dev
_CONFIG: line 9: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
project-devproject-staging
project-devproject-staging
dog
0 - dog

$ bash --version
GNU bash, version 4.4.0(1)-release (x86_64-apple-darwin15.6.0)

如何映射用户输入' dev'到我的项目名称'在bash?谢谢

1 个答案:

答案 0 :(得分:3)

通过升级bash解决了这个问题。 $BASH_VERSION打印您的真实版本

brew unlink bash
brew update && brew install bash

test.sh:

declare -A instance_map=( ["dev"]="project-dev" ["stage"]="project-staging" ["prod"]="project" )

echo ${instance_map["dev"]}
echo ${instance_map["stage"]}