S
S
Sergey Sokolov2017-09-30 23:18:43
linux
Sergey Sokolov, 2017-09-30 23:18:43

How to get the number of characters in a unicode string in bash?

The string contains Cyrillic and English letters. How to get number of letters in bash script?
For example: Need to get . Tried:
TEST="йцукенasdf"
10

CHRLEN=$(wc -m <<< $TEST)
printf "%s has %d characters\n" "$TEST" "$CHRLEN"

For some reason it shows 11, 1 more than the truth. returns 16 - Cyrillic counts one for two.
CHRLEN=${#TEST}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xotkot, 2017-10-01
@sergiks

For some reason it shows 11, 1 more than the truth.

it also considers a line break as a character
then just add the required encoding before the command, for example: or from the examples above

S
Saboteur, 2017-09-30
@saboteur_kiev

$ TEST="ytsukenasdf"
$ echo ${#TEST}
10

M
Maxim Moseychuk, 2017-10-01
@fshp

<<<adds a newline character to the end.

CHRLEN=$(echo -n $TEST | wc -m)
printf "%s has %d characters\n" "$TEST" "$CHRLEN"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question