bash: using a string variable in grep

Hi,

I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg...

grep 'pattern of several words' filename

I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable:

variable=$@

I guess this is an array variable consisting of arg1, arg2 etc. I try to use it later on data piped into grep...

cat file | grep $variable

This doesn't work -- grep sees arg1 and arg2 of my variable separately and interprets it as 'grep pattern[arg1] filename[arg2]'. Consequently it errors saying "file [arg2] not found".

So I tried using quotes on the script line, as I would on the command line...

cat file | grep '$variable'

but it then seems to treat $variable literally and greps on the actual name of the string, so returns no results.

I've tried backslashed quotes....

cat file | grep \'$variable\'

but that doesn't work either, it thinks arg2 is a filename again.

Any ideas on the way to do this?

Thanks,
Adrian.

You missed the good old double-quotes....

fgrep "$variable" file

assuming that you need to search the string as a string and not as a regular expression. Drop the "f" if you want to search with a regular expression.

Thanks, that worked!