Assign grep count to variable

后端 未结 3 1954
长发绾君心
长发绾君心 2021-02-05 02:23

How to assign the result of

grep -c \"some text\" /tmp/somePath

into variable so I can echo it.

#!/bin/bash
some_var = grep -c         


        
3条回答
  •  自闭症患者
    2021-02-05 02:42

    some_var=$(grep -c "some text" /tmp/somePath)
    

    From man bash:

       Command substitution allows the output of a command to replace the com‐
       mand name.  There are two forms:
    
              $(command)
       or
              `command`
    
       Bash performs the expansion by executing command and replacing the com‐
       mand substitution with the standard output of  the  command,  with  any
       trailing newlines deleted.
    

提交回复
热议问题