I'm trying to find the number of times a string is repeated in a file, at the same time i've to store it in a variable.
When i use the command (cat filename | grep -c '123456789'
) , it displays the count correctly but when i use the below command it shows as command not found.
var =$(cat filename | grep -c '123456789')
echo $var
Can u let me know where i'm wrong ?
To store the output of a command in a variable, you can use the shell command substitution feature in the forms below: variable_name=$(command) variable_name=$(command [option ...] arg1 arg2 ...) OR variable_name='command' variable_name='command [option ...]
The dv command displays the names and values of all local variables in the current scope.
Don't use spaces around the =
sign:
var=$(cat filename | grep -c '123456789')
Read at least the Bash Prog Intro Howto
But you've got a useless use of cat
so code simply
var=$(grep -c '123456789' filename)
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