How to count number of words from String using shell

后端 未结 6 1563
梦谈多话
梦谈多话 2020-12-16 09:44

I want to count number of words from a String using Shell.

Suppose the String is:

input=\"Count from this String\"

Here the delimit

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-16 10:11

    echo "$input" | wc -w
    

    Use wc -w to count the number of words.

    Or as per dogbane's suggestion, the echo can be got rid of as well:

    wc -w <<< "$input"
    

    If <<< is not supported by your shell you can try this variant:

    wc -w << END_OF_INPUT
    $input
    END_OF_INPUT
    

提交回复
热议问题