EN
Bash - get string length
3 points
In this article, we would like to show you how to get string variable length in Bash.
Quick solution:
xxxxxxxxxx
1
length=${#string}
In this example, we present how to use #
operator to get string
variable length. Then we use echo
command to display the result in the console.
Example script.sh
file:
xxxxxxxxxx
1
2
3
string="abc"
4
length=${#string}
5
6
echo "Length: $length"
Run in command line:
xxxxxxxxxx
1
./script.sh
Output:
xxxxxxxxxx
1
Length: 3