Find the length of string

From Linux Bash Shell Scripting Tutorial Wiki
Jump to navigation Jump to search

To find the length of any given string we use the ${#str_var}} special bash format:

${#str_var}
foo="This is a test"
echo "${#foo}

Finding the length of string example

#!/bin/bash
STR="This is a very long string and I want to know the length automatically."
 
echo "$STR"
echo "Length of your string: ${#STR} chars long."

See also