Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String length of bash

Tags:

bash

shell

I have written this to find the length of the string, but it doesn't show the length of the string.

Is there something wrong with what I have written? I'm just a beginner in bash.

Str=abcdefghj

echo "Str is:" `expr length $Str` "characters long"
like image 823
user2443218 Avatar asked Jul 19 '13 16:07

user2443218


People also ask

How do I get the length of a string in Bash?

We can use the # operator to get the length of the string in BASH, we need to enclose the variable name enclosed in “{ }” and inside of that, we use the # to get the length of the string variable. Thus, using the “#” operator in BASH, we can get the length of the string variable.

How do I find the length of a string in Linux?

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. Output: The following output will appear after running the above command.

What is the length of the string?

The length or size of a string means the total number of characters present in it. For Example: The string “Geeks For Geeks” has 15 characters (including spaces also).


1 Answers

This can be done natively in bash, no need to resort to expr

echo ${#Str}
like image 113
iruvar Avatar answered Sep 27 '22 23:09

iruvar