Grepping for variable in brackets

I have a script which reads a number out of a log file. The pertinent line is this:

cat /tmp/listofnumbers

When I run cat /tmp/listofnumbers what I am seeing is [9878] [6376] on each line.
I am trying to make the script read from that file and grep for a variable like the following line should do:

cat /tmp/listofnumbers |grep "\[$var\]"

But that doesn't work. I can echo the variable number but the grep will never return it in the file. I have tried using $( and other tricks but it doesn't work. Any suggestions would be greatly appreciated.

How do you set the $var variable? I suspect the 'var' contains some other characters, since the grep line works as expected:

$ var=9878; echo '[9878] [6376]' | grep "\[$var\]"
[9878] [6376] 

If you post your script, you would have a much better chance of getting help.

Thanks for your suggestion, but I did get it solved:
I tried this:

cat /usr/openv/netbackup/logs/bptm/log.030713 |egrep "\[$bpx\]"

egrep worked for me don't know why grep didn't.

I think you're kidding yourself.

The grep and egrep utilities should treat these expressions the same way. We need to see how you are setting bpx (or in your earlier example var ) and a sample of the lines in your input file that you expect the expression to match to figure our what is really going on here.

And, for the record:

cat file|grep expression

is much more efficiently written as:

grep expression file

It should work, as mirni demonstrates. Here is another demonstration.

$ cat temp.x
[123]
[456]
$ x=123
$ cat temp.x | grep "\[$x\]"
[123]
$ cat temp.x | grep -v "\[$x\]"
[456]

I don't think it's a good idea to say "grep doesn't work, but don't know why, but egrep works for some unknown reason, so I'll use egrep". Better to under WHY grep doesn't work for you. There is always a reason. I hope you can find the reason.