How to assign the result of
grep -c "some text" /tmp/somePath
into variable so I can echo it.
Thanks!
如何将grep -c“some text”/ tmp / somePath的结果分配给变量,以便我可以回应它。谢谢!
#!/bin/bash
some_var = grep -c "some text" /tmp/somePath
echo "var value is: ${some_var}"
I also tried: some_var = 'grep -c \"some text\" /tmp/somePath'
我也尝试过:some_var ='grep -c \“some text \”/ tmp / somePath'
But I keep getting: command not found
但我一直得到:没找到命令
Thanks
谢谢
3 个解决方案
#1
50
To assign the output of a command, use var=$(cmd)
(as shellcheck automatically tells you if you paste your script there).
要分配命令的输出,请使用var = $(cmd)(因为shellcheck会自动告诉您是否将脚本粘贴到那里)。
#!/bin/bash
some_var=$(grep -c "some text" /tmp/somePath)
echo "var value is: ${some_var}"
#2
16
Found the issue
Its the assignment, this will work:
找到问题它的任务,这将工作:
some_var=$(command)
While this won't work:
虽然这不起作用:
some_var = $(command)
Thank you for your help! I will accept first helpful answer.
感谢您的帮助!我会接受第一个有用的答案。
#3
3
some_var=$(grep -c "some text" /tmp/somePath)
From man bash
:
来自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.
#1
50
To assign the output of a command, use var=$(cmd)
(as shellcheck automatically tells you if you paste your script there).
要分配命令的输出,请使用var = $(cmd)(因为shellcheck会自动告诉您是否将脚本粘贴到那里)。
#!/bin/bash
some_var=$(grep -c "some text" /tmp/somePath)
echo "var value is: ${some_var}"
#2
16
Found the issue
Its the assignment, this will work:
找到问题它的任务,这将工作:
some_var=$(command)
While this won't work:
虽然这不起作用:
some_var = $(command)
Thank you for your help! I will accept first helpful answer.
感谢您的帮助!我会接受第一个有用的答案。
#3
3
some_var=$(grep -c "some text" /tmp/somePath)
From man bash
:
来自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.