couting occurences of a character inside a string and assigning it to a variable

echo "hello123" | tr -dc '[0-9]' | wc -c

using this command i can count the no of times a number from 0-9 occurs in the string "hello123"
but how do i save this result inside a variable?

if i do
x= echo "hello123" | tr -dc '[0-9]' | wc -c

that does not work...plz suggest..thanks

x=`echo "hello123" | tr -dc '[0-9]' | wc -c`

And don't put spaces around the equal sign.

1 Like

thanks so much...works perfectly...thanks a lot :slight_smile:

$ echo "abc123" | fold -w 1 | grep -c "[0-9]"
3