Linux shell programming - get the length of a variable

That is to say, now there is a variable $name, I want to obtain the length of its value, how should I do it?

In this way: echo $name | wc -c | read asd
In this way, the length of the name variable is read to the asd variable, but it will be 1 more than the actual length.

There is a better way: echo $name | awk '{printf("% d",length($1))} | read asd


#!/bin/sh
echo -n "Input ID (Ex. : ca0001): "
read STR
name=$STR
echo ""
echo $name | awk '{printf( "%d",length($1))}'| read length
echo $length
while [ "$length" != "6" ]
do
echo "Please input ID in correct format!"
read STR
name=$STR
done
echo " succeed"

What is the result of echo $length?

Another way of writing:
length=`echo $name | awk '{printf("%d",



# echo $name | awk '{printf("%d",length($0))}' | read asd
# ec

Guess you like

Origin blog.csdn.net/m0_37449634/article/details/131454872