Issue while storing grep result

Hi All,

Command: grep -i -n "rule" *.err *.log | grep -v "SP_RULE"
The above command when run gives me two -three lines of output.
Now I am storing the result of the grep command
ie,
ruleerrors=`grep -i -n "rule" *.err *.log | grep -v "SP_RULE"`
Now when I echo the value of ruleerrors, I am getting a large result similar to result of �ls� command along with the actual desired output.

What can be cause ?

Add double quotes around the variable name to avoid wildcard expansion.

Thanks era.
It worked.

What is the difference between echo $ruleerrors and echo "$ruleerrors"?

Thank you

That is pretty much the difference. See a shell quoting tutorial such as UNIX Shell Quote for a detailed explanation.

Thanks Era (for the link).

One thing is not clear to me is ...

grep returned multiple rows and the output is redirected to a variable then
-will it be saved in a single line (with hidden characters)? or in multiple lines ?

Could you help me if I am missing some thing from your message?
you said "Add double quotes around the variable name to avoid wildcard expansion"

Thank you.

The value is saved with newlines and everything. echoing it back without quoting will flatten any whitespace to single spaces. (This is a feature of the shell and its quoting mechanisms, not of echo.) To make sure you see the real actual value, examine the variable with set.

sh$ var=`perl -e 'print "Output\nwith\ \ \ spaces\t\ \nand newlines\n"'`
sh$ echo $var
Output with spaces and newlines
sh$ echo "$var"
Output
with   spaces	 
and newlines
sh$ set | grep var=
var=$'Output\nwith   spaces\t \nand newlines'

This is with bash; output will probably be slightly different with other shells.

Notice that the final trailing newline is chomped off by the shell.