将grep计数分配给变量

问题描述:

如何分配

grep -c "some text" /tmp/somePath

变成变量,以便我可以回显它.

into variable so I can echo it.

#!/bin/bash
some_var = grep -c "some text" /tmp/somePath
echo "var value is: ${some_var}"

我也尝试过:

some_var = 'grep -c \"some text\" /tmp/somePath'

但是我不断得到:command not found.

要分配命令的输出,请使用var=$(cmd)(如 shellcheck 自动告诉您是否将脚本粘贴到那里.

To assign the output of a command, use var=$(cmd) (as shellcheck automatically tells you if you paste your script there).

#!/bin/bash
some_var=$(grep -c "some text" /tmp/somePath)
echo "var value is: ${some_var}"