Sometimes in the Linux operating system to calculate the length of a string, through the query data to the current shell to get the length of the string of various methods to share here, the method is as follows:
Method 1: Use the wc-l command
Wc-l can get to the length of the current line, so the string for a single line can be obtained in this simple way, and wc-l is the number of rows to get the current string content.
Copy Code code as follows:
Method 2:expr Length string
Using expr length to get the length of a string
Method 3:awk Gets the number of domains, but if there is a problem with a length greater than 10 characters, you need to confirm later
Copy Code code as follows:
echo "abc" |awk-f "" ' {print NF} '
Method 4: Get the length of the string by awk+length
Copy Code code as follows:
echo "Alex" |awk ' {print length ($)} '
Method 5: By means of the Echo ${#string} (Note: The string here is the variable name of that character)
Copy Code code as follows:
There should be other ways to get the string length in the shell, please know the other way of the reply to add, there is a problem where we correct!