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"
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.
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.
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).
This can be done natively in bash, no need to resort to expr
echo ${#Str}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With