Skip to main content
Quick Tip

How to Find Length of String in Bash [Quick Tip]

Here are various ways to calculate the length of a string in bash scripts.

Abhishek Prakash

If you are dealing with strings in bash, you may need to know the length of string.

Thankfully, getting length of string in bash is super simple. Let's say you have a string named my_string. Its length can be extracted as:

${#my_string}

Here's an example to explain things better:

abhishek@handbook:~$ my_string="abhishek"
abhishek@handbook:~$ echo "length is ${#my_string}"
length is 8

That was easy, wasn't it? You can save this string length to another variable and use it later:

abhishek@handbook:~$ my_string="abhishek"
abhishek@handbook:~$ length=${#my_string}
abhishek@handbook:~$ echo "String length is $length"
String length is 8

Like most other things in Linux, this is not the only way to calculate the length of strings in Bash.

Get string length in bash using expr command

Let's see some other commands that could help you to test the string length in bash.

One of these commands is the expr command. It has several options that are useful for string options. Among them, length gives you the length of a string.

abhishek@handbook:~$ expr length "my string"
9

Since the expr command outputs the length, you should store it in a variable using command substitution.

#!/bin/bash

str="my string"
length=$(expr length "$str")
echo "Length of my string is $length"

Now if you run this bash shell script, it will print the length:

abhishek@handbook:~$ bash string.sh 
Length of my string is 9

Use awk to get length of string

AWK is super versatile command for editing text in Linux command line. You can use it to calculate the string length as well.

You'll have to use echo command and then use pipe redirection to parse it with awk:

abhishek@handbook:~$ echo "my string" | awk '{print length}'
9

As you can see, unless you are familiar with awk command basics, it's not easy to remember the syntax.

Bash Tutorials for Beginners: Start Learning Bash Scripting
Here’s a collection of bash tutorials that will teach you bash shell scripting from the beginning. You’ll learn all the basics of bash scripting.

Using wc command to calculate string length

Another way to get string length in Linux is using the wc command. Now wc command is used for counting number of lines, characters in a file.

You can echo the string and pipe it to wc command. The -m option gives the character count.

abhishek@handbook:~$ echo -n "my string" | wc -m
9

Notice the -n option with echo command? That is important because echo automatically adds a new line character \n at the end and it will increase the length of the string by one. With -n option, echo command doesn't add new line character.

You can use command substitution to store the string length in a variable as you saw in previous examples.

#!/bin/bash

str="my string"
length=$(echo -n "my string" | wc -m)
echo "Length of my string is $length"

Personally, ${#string} is my preferred way of getting string length. Easier to remember.

How about you? Which method do you prefer?

Abhishek Prakash