在MacOS上的bash脚本中使用关联数组 [重复] 。

问题描述 投票:0回答:1

我使用的是MacOS ( Mojave 10.14.6 ),想写一个bash脚本。 我已经安装了最新版本的bash - 5.0.16(1)-release (x86_64-apple-darwin18.7.0) - 使用 brew install。

我没有权限更新 etcshells 文件,所以我尝试使用

#!/usr/bin/env bash
bash --version

在我的脚本代码中

而这也会返回上面的版本- 5.0.16(1)-release (x86_64-apple-darwin18.7.0) -但是,当我在同一个脚本中尝试使用

declare -A svcCount

我得到以下错误信息

declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

这似乎表明,该脚本仍在使用Mac的默认bash 3.x版本。

我应该如何修改脚本,使我能够使用上述的关联数组定义结构?

謝謝你

bash associative-array macos-mojave
1个回答
1
投票

请将其保存为 theScript 如果你想使用 自酿 bash:

#!/usr/local/bin/bash
echo $BASH_VERSION
declare -A svcCount

然后用以下方法使其可执行。

chmod +x theScript

然后用:

./theScript
© www.soinside.com 2019 - 2024. All rights reserved.