0

I just want to echo an incremented string length. For example:

STR="test"
echo ${#STR}

It prints 4, but I want to print 5.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
GeoCap
  • 505
  • 5
  • 15

2 Answers2

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

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

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

The length of the string cannot be changed without modifying the string. If you want to add another character at the beginning, try

STR="a$STR"

Now ${#STR} is 5, because STR is atest (five characters).

If you want to perform arithmetic in the shell, use an arithmetic expression:

echo "$((1+${#STR}))"
tripleee
  • 175,061
  • 34
  • 275
  • 318