如何分配结果
grep -c“一些文本”/ tmp / somePath
到variables,所以我可以回显它。
谢谢!
#!/bin/bash some_var = grep -c "some text" /tmp/somePath echo "var value is: ${some_var}"
我也试过:some_var ='grep -c \“一些文本\”/ tmp / somePath'
但是我不断收到:command not found
谢谢
要分配一个命令的输出,使用var=$(cmd)
(因为shellcheck会自动告诉你是否粘贴你的脚本)。
#!/bin/bash some_var=$(grep -c "some text" /tmp/somePath) echo "var value is: ${some_var}"
发现问题
它的任务,这将工作:
some_var=$(command)
虽然这不起作用:
some_var = $(command)
感谢您的帮助! 我会接受第一个有用的答案。
some_var=$(grep -c "some text" /tmp/somePath)
从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.