gpt4 book ai didi

shell - 变量值加1(shell编程)

转载 作者:太空宇宙 更新时间:2023-11-03 16:41:29 25 4
gpt4 key购买 nike

我似乎无法将变量值增加 1。我查看了 tutorialspoint's Unix / Linux Shell Programming tutorial但它只显示了如何将两个变量加在一起。

我尝试了以下方法,但它们不起作用:

i=0

$i=$i+1 # doesn't work: command not found

echo "$i"

$i='expr $i+1' # doesn't work: command not found

echo "$i"

$i++ # doesn't work*, command not found

echo "$i"

如何将变量的值递增 1?

最佳答案

您可以使用 arithmetic expansion像这样:

i=$((i+1))

declare i 作为整数变量并使用 += 运算符递增其值。

declare -i i=0
i+=1

或使用 (( construct .

((i++))

关于shell - 变量值加1(shell编程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21035121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com