How to find String Length in Bash Script

Bash String Length

Bash String Length – In this tutorial, we shall learn ways to find the length of string in bash script.

To find String Length in Bash Scripting use one of the following syntax :

  1. ${#string}
  2. `expr length "$string"`Observe the quotes around $string . If your string has spaces, double quotes around $string  is kind of mandatory, else in other cases you may ignore. However, to be on the safe side, always try including double quotes around $string .
  3. `expr "$string" : '.*'`

 

Examples to find String Length in Bash

Following are the examples that demonstrate different ways to find length of string in bash shell scripting

Example 1

Run the above program in Terminal
 

Example 2

Run the above program in Terminal
 

Example 3

Run the above program in Terminal
 

Conclusion :

In this Bash TutorialBash String Manipulation Examples – Bash String Length, we have learnt to find length of String in Bash Shell Scripting.