5 ways to calculate the length of a string in a shell script _linux shell

Source: Internet
Author: User

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:
echo "ABC" |wc-l

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:
Name=alex
echo ${#name}

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.