Bash 增加字符串的长度

Bash increment length of string

我只想回显增加的字符串长度。例如:

STR="test"
echo ${#STR}

它打印 4,但我想打印 5。

STR="test"
count=${#STR}
((count++))
echo $count

((count++)): How to increment a variable in bash?

不修改字符串就不能改变字符串的长度。如果你想在开头添加另一个字符,试试

STR="a$STR"

现在${#STR}是5,因为STRatest(五个字符)。

如果要在 shell 中执行算术运算,请使用 arithmetic expression:

echo "$((1+${#STR}))"