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 "some text" /tmp/somePath echo "var value is: ${some_var}"
I also tried:
some_var = 'grep -c \"some text\" /tmp/somePath'
But I keep getting: command not found
.
Grep works well with standard input. This allows us to use grep to match a pattern from a variable. It's is not the most graceful solution, but it works. To learn more about standard streams (STDIN, STDOUT, & STDERR) and Pipelines, read "Linux I/O, Standard Streams and Redirection".
You can use: targets=($(grep -HRl "pattern" .)) Note use of (...) for array creation in BASH. Also you can use grep -l to get only file names in grep 's output (as shown in my command).
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}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With