Find Length of String in Bash

29/12/2020
The total number of characters of any string data indicates the length of the string. When we work with string data then it is important to count the length of the string for various programming tasks. The built-in function exists to count the total number of characters in many programming languages. But bash has no this type of built-in function. The length of the string can be counted in bash in multiple ways. How you can find out the length of a string data in bash is shown in this tutorial by using different examples.

Syntax:

Any of the following syntaxes can be followed to count the length of string.

${#strvar}
expr length $strvar
expr${strvar}”:’.*
$strvar | wc -c
$strvar |awk ‘{print length}’

The above syntaxes show that length of the string can be counted by any bash command or without any command. ‘#‘ symbol can be used to count the length of the string without using any command. `expr` command can be used by two ways to count the length of a string. Without `expr`, `wc` and `awk` command can also be used to count the length of a string. The uses of the mention commands and ‘#’ symbol for counting the length of the string is shown in the next part of this tutorial.

Example-1: Using the‘#’ symbol to count the length of a string

The most commonly used and simple way to count the length of a string is to use “#” symbol. The following commands will assign a value to the variable, $string and print the total number of characters of $string.

$ string="Learn Bash Programming with LinuxHint"
$ echo ${#string}

Output:

The following output will appear after running the above command.

Example-2: Using `expr` to count the length of a string

Another way to count the length of a string is to use `expr` command with length keyword. The following commands will assign a value to the variable, $string, store the length value to the variable, $len and print the value of $len.

$ string="Hypertext Markup Language"
$ len=`expr length "$string"`
$ echo "The length of string is $len"

Output:

The following output will appear after running the above command.

Create a bash file named “len1.sh” and add the following script. Here, a string value will be taken from the user and the length of the string value will be counted by using `expr` command that will be printed later.

len1.sh

#!/bin/bash
echo  “Enter a string:”
read strval
len=`expr "$strval" : ‘.*’`
echo "The length of the input string is $len"

Run the script.

$ bash len1.sh

Output:

Here, “I like Programming” is taken as input and the length of the string is 18.

Example-3: Using `wc` to count the length of the string

Create a bash file named “len2.sh” and add the following script. This script will read the first command-line argument into the variable $strval and count the length of $strval by using `wc` command that will be printed later.

len2.sh

#!/bin/bash
strval=$1
len=`echo $strval | wc -c`
echo "The length of the first command-line argument is $len"

Run the script with one command-line argument.

$ bash len2.sh “Hello World”

Output:

The length of “Hello World” is 12 that is printed as output.

Example-4: Using `awk` to count the length of string

Create a bash file named “len3.sh” and add the following script. Here, the username will be taken as input and check the length of $username is less than 6 or not. If the length is less than 6 then the output will “Invalid username” otherwise the output will “Valid username”.

len3.sh

#!/bin/bash
echo "Enter the username"
read username
len=`echo $username |awk ‘{print length}’`
if [ $len -lt 6 ]; then
echo "Invalid username"
else
echo "Valid username"
fi

Run the script.

$ bash len3.sh

Output:

Here, when “fahmida” is taken as the username then it is valid and when “lily” is taken as the username then it is invalid.

Conclusion:

Different ways of counting the length of a string in bash are shown in this tutorial by using various examples. The user can apply any of the mentioned ways to find out the length of the string.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Return a String from Bash Functions

Use of BASH function that returns a value Bash Functions can’t return values like other standard programming languages....
28/12/2020

How to make BASH suit you better

BASH has a simple standard setup which is great but you may want more! Many computer users avoid the command line, because...
29/12/2020

Bash brace expansion

Bash uses brace expansion to generate a sequence of strings from the terminal or by using any bash script. A sequence of...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024