wc -c is not working properly for Strings
When I count the number of character in a string using
`wc -c $Str` it always gives a one character extra to me. e.g : str="Good" ; len=`echo $str | wc -c` it results in len=5 not 4. |
When you echo the string, echo appends a newline character which is counted by wc.
Suppress the newline to do this with echo, or better might be to use the shell's built in operator for getting the string length, ${#var}. |
Quote:
But first, I would like to make you notice that wc with argument "-c" will count number of bytes, not number of characters. You are getting the correct number of characters (counting the newline) because every character is 1 byte each. But it will not work with multibyte characters like UTF-8. Code:
echo -n "Good" | wc -c |
All times are GMT -5. The time now is 10:33 PM. |