Shell scripting, basic sting manipulation, how to find the length of a string
# ${#my_string}
The above will print the length of the sting variable
Now lets see a example how to calculate the length of a string?
Example:
# vi find_string_length.sh
#! /bin/bash
str_Length="Welcome to the w3calculator"
echo ${#str_Length}
Output:
# sh find_string_length.sh
27
The lenght or number of character of "Welcome to the w3calculator" is 27
Other way to calculate the length of a string is
# echo "welcome to w3calculator" | wc -c
24
You can also use this in your shell scripting as below.
# vi character_count.sh
str_Length="Welcome to the w3calculator"
length=$(echo $str_Length | wc -c)
echo "$length"
# sh character_count.sh
28
|