Sometimes in Linux operating system, you need to calculate the length of a certain string. By querying information, I sorted out various methods to obtain the length of a string in the shell. I will share it with you here. The methods are as follows:
Method 1: Use the wc -L command
wc -L can get the length of the current line, so you can use this simple method for single lines to get the strings. In addition, wc -l can get the number of lines in the content of the current string.
Method 2: expr length string
Use expr length to get the length of the string
Method 3: awk gets the number of fields, but if there is a problem if it is greater than the length of 10 characters, it needs to be confirmed later.
Method 4: Get the string length through awk+length
Method 5: By echo ${#string} (Note: string here is the variable name of the string)
echo ${#name}
There should be other ways to get string length in the shell. Please add to the reply of other methods. If you have any problems, please correct me if there are any problems!