I'm working on a Bash script and the length of the string contained in a certain variable is one of my conditions. The current string is W5u7TBTzF17GGFV8DBJHvgAAAAI
. Initially I've count the string length by the help of wc -c
:
$ VAR='W5u7TBTzF17GGFV8DBJHvgAAAAI'; echo "$VAR" | wc -c
28
But my script condition [[ ${#VAR} -eq 28 ]]
never pass. Then I decided to count the characters on by one. Indeed the string length is 27 characters, also the value of ${#VAR}
is 27:
$ echo "${#VAR}"
27
So I'm in wondering - where does this difference come from?
It's the way
echo
works. Now doYou get
But do
echo -n koko
and you getSo
wc
is capturing thenewline character
too. UseTo get the desired result. The
echo
command will add thenewline
character, so that gets counted too. To remove this and get the real count use the-n
option.