Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store the value of cat command in the variable var?

Tags:

unix

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 ?

like image 362
Jackie James Avatar asked Jun 05 '12 04:06

Jackie James


People also ask

How do you store results of command in a variable?

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 ...]

Which command should I use to display a variable?

The dv command displays the names and values of all local variables in the current scope.


1 Answers

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)
like image 195
Basile Starynkevitch Avatar answered Nov 04 '22 10:11

Basile Starynkevitch