The length of a string is the number of characters in it. It is straightforward to know the length of a string for normal text.
This post explains ways to count the number of characters in a string with UTF encoding.
String length in Bash
There are several ways we can find the length of a string.
The first way, use the ${#variable} syntax.
In this, find the string variable length.
msg="Hello World."
count=${#msg}
echo $count # 12
Output:
12
The second way, using the wc -m
pipe symbol with an echo string variable.
It can find the length using a string or variable.
echo -n 'Hello World.' | wc -m; # 12
result=`echo -n $msg | wc -m`
echo $result # 12
result1=`echo $msg | wc -m`
echo $result1 # 12
Output:
12
12
12