【发布时间】:2017-10-12 09:12:14
【问题描述】:
我有一个与 id 完全一样的点文件名称关联数组:
declare -A ids=(
[".steve"]="1 4 5 6 10"
[".john"]="3 4 5 1 11"
...
)
当我运行这段代码时,我得到:
./declare_ids.sh: line 23: .steve: operand expected (error token is ".steve")
这个错误看起来非常非常模糊。我不明白发生了什么事。我对 bash 很陌生,并且刚刚学习了 bash v4 中的关联数组。有人可以帮忙吗?
编辑:
此脚本中的 shebang 行是 #!/bin/bash。我在 OS X 上的 zsh 终端内运行它,并通过 brew install bash 安装了 bash。
bash --version 说:
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin15.6.0) │
Copyright (C) 2016 Free Software Foundation, Inc. │
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> │
│
This is free software; you are free to change and redistribute it. │
There is NO WARRANTY, to the extent permitted by law.
当我将bash --version 放在declare -A 行之前也是这种情况。
而which bash 返回/usr/local/bin/bash。只需键入./declare_ids.sh,即可在终端内调用该脚本。
再次编辑:
正如已经说过的,回显$BASH_VERSION 与bash --version 不同。 $BASH_VERSION 是 3.2.57(1)-release。
有没有办法让 /bin/bash 升级?我也在 ubuntu circleCI 盒子上使用这个脚本,所以任何硬编码为 brew 所做的事情都会很糟糕。
【问题讨论】:
-
命令
bash --version启动一个新的bash 实例并返回其版本。要查找运行脚本的 bash 版本,请添加命令echo $BASH_VERSION并向我们显示结果。 -
你的 she-bang 行说
/bin/bash。如果which bash表示/usr/local/bin/bash,那么(可能)是不同的 bash 版本。 -
您可以比较
/bin/bash --version(脚本运行的内容,可能是旧 Bash)和/usr/local/bin/bash --version,可能是新 Bash。使用bash ./script运行脚本使用新的 Bash,运行它./script使用旧的 Bash。这是我的猜测。 -
@John1024 很棒,更新了我的回复。
-
我投票决定将其作为副本关闭,但我在这里给出的答案丢失了,所以我在那里添加了它。
标签: arrays bash associative-array