Counting words and characters in Bash without wc

I have a variable set like this:

sentence="a very long sentence with multiple       spaces"

I need to count how many words and characters are there without using other programs such as wc.

I know counting words can be done like this:

words=( $sentence )
echo ${#words[@]}

But how do I count the characters including spaces?


Source: bash

Leave a Reply