关联数组:错误“声明:-A:无效选项"; [英] Associative arrays: error "declare: -A: invalid option"
问题描述
我编写了一个在bash(v 4)中使用关联数组的脚本.
I've written a script that uses associative arrays in bash (v 4).
它在使用4.1.5(1)-release
的本地计算机上正常工作.
It works fine on my local machine which is using 4.1.5(1)-release
.
在生产计算机上,使用4.1.0(1)-release
声明assoc数组的以下行失败:
On the production machine, using 4.1.0(1)-release
the following line, which declares the assoc array, fails:
declare -A uniqjars
显示以下消息:
/script.sh: line 11: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
我对这是bash 4的一般功能有印象吗?
I was under the impression this was a general bash 4 feature?
在生产机器上使用bash的人中,它讨论了使用-A
的方法,因此我认为应该可以工作.
In the man for bash on the production machine it discusses using -A
so I assume it should work.
关联数组是使用以下方法创建的
declare -A name
.
Associative arrays are created using
declare -A name
.
我可以通过打印出echo 'bash -version
的值来确认脚本使用的是正确的bash版本.
I can confirm the script is using the right version of bash by printing out the value of echo 'bash -version
.
我做错了什么?
推荐答案
请确保在shell脚本顶部(#!/bin/bash
或其他版本)作为解释器调用的bash版本也是版本4.在做:
Make sure the version of bash being invoked as interpreter at the top of your shell script (#!/bin/bash
or whatever) is also version 4. If you're doing:
bash --version
它会给您v4,请执行which bash
来检查其位置.
and it's giving you v4, do a which bash
to check it's location.
这篇关于关联数组:错误“声明:-A:无效选项";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!