Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

bash count occurrences of character in string

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 
 
PREVIOUS NEXT
Tagged: #bash #count #occurrences #character #string
ADD COMMENT
Topic
Name
6+8 =