countz() { echo "$1" | tr -cd "$2" | wc -c } declare -l myString="1-2-3-4-5-6-7-8-9-0" myChar="-" echo $(countz "$myString" "$myChar") #outputs: 9