如何从BASH中的数组项中减去1? [英] How do I subtract 1 from an array item in BASH?
本文介绍了如何从BASH中的数组项中减去1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在BASH中有一个附带项目,很有趣,而且我有以下代码片段( ARRAY [0]
为8):
I have a side-project in BASH for fun, and I have this code snippet (ARRAY[0]
is 8):
while [ $ALIVE == true ]; do
$ARRAY[0] = ${ARRAY[0]} - 1
echo ${ARRAY[0]}
done
但是,它返回此错误:
line 16: 8[1]: command not found
我刚开始在BASH中工作,所以我可能犯了一个明显的错误,但是我一直在搜索并搜索此类问题的答案,但没有结果.
I just started working in BASH, so I might be making an obvious mistake, but I've searched and searched for an answer to a problem like this and came up with no result.
推荐答案
最小的变化就是:
ARRAY[0]=$(( ${ARRAY[0]} - 1 ))
注意:
- 要分配给变量的名称前没有
$
(foo =
,而不是$ foo =
) - 作业上
=
周围没有空格 -
$(())
是用于输入数学上下文(并扩展到该运算结果)的语法.
- No
$
before the name of the variable to assign to (foo=
, not$foo=
) - No spaces around the
=
on the assignment $(( ))
is the syntax to enter a math context (and expand to the result of that operation).
这篇关于如何从BASH中的数组项中减去1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文