如何分配结果
grep -c“some text”/ tmp / somePath
进入变量所以我可以回应它。
谢谢!
#!/bin/bash
some_var = grep -c "some text" /tmp/somePath
echo "var value is: ${some_var}"
我也尝试过: some_var ='grep -c \“some text \”/ tmp / somePath'
但我一直得到:命令未找到
答案 0 :(得分:53)
要分配命令的输出,请使用var=$(cmd)
(因为shellcheck会自动告诉您是否将脚本粘贴到那里)。
#!/bin/bash
some_var=$(grep -c "some text" /tmp/somePath)
echo "var value is: ${some_var}"
答案 1 :(得分:23)
发现了这个问题
它的任务,这将工作:
some_var=$(command)
虽然这不起作用:
some_var = $(command)
感谢您的帮助!我会接受第一个有用的答案。
答案 2 :(得分:3)
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.