bash关联词典不起作用(声明:-A:无效选项) [英] bash associative dictionary not working (declare: -A: invalid option)

查看:112
本文介绍了bash关联词典不起作用(声明:-A:无效选项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了每个bash词典的示例,但没有一个有效.我现在有

I have followed each bash dictionary example, and none will work. I have now

_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

由于某种原因,在遵循每个示例之后,我得到了

For some reason, after following each example, I get

$ ./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)

如何在bash中将用户输入"dev"映射到"myprojectname"?谢谢

How do I map the user input 'dev' to 'myprojectname' in bash? Thank you

推荐答案

此问题已通过升级bash得以解决. $BASH_VERSION打印出您的真实版本

The problem was fixed by upgrading bash. $BASH_VERSION prints your true 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"]}

这篇关于bash关联词典不起作用(声明:-A:无效选项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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