Count occurrences of a char in a string using Bash

前端 未结 7 1649
臣服心动
臣服心动 2020-11-27 11:26

I need to count the number of occurrences of a char in a string using Bash.

In the following example, when the char is (for example) t, it

7条回答
  •  被撕碎了的回忆
    2020-11-27 11:59

    I Would suggest the following:

    var="any given string"
    N=${#var}
    G=${var//g/}
    G=${#G}
    (( G = N - G ))
    echo "$G"
    

    No call to any other program

提交回复
热议问题