Count occurrences of a char in a string using Bash

前端 未结 7 1631
臣服心动
臣服心动 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

    Building on everyone's great answers and comments, this is the shortest and sweetest version:

    grep -o "$needle" <<< "$haystack" | wc -l

提交回复
热议问题