0

I want to be able to use grep with variable and string combined for searching.

test="HTTP/[0-9].[0-9]" 
grep '$test [0-9][0-9][0-9] ||' some_file

Tried to put the variable in ${test} but that did not help.

How can i combine grep arguments with a variable and a string?

SScast
  • 59
  • 1
  • 10

1 Answers1

6

You have to enclose your pattern in double quotes to enable variable expansion :

test="HTTP/[0-9].[0-9]" 
grep "$test [0-9][0-9][0-9] ||" some_file

test='expanded'
echo '$test' # will echo $test
echo "$test" # will echo expanded
Aaron
  • 24,009
  • 2
  • 33
  • 57