Article 5NPH5 [SOLVED] Can I use variables as patterns for grep?

[SOLVED] Can I use variables as patterns for grep?

by
pbadillo555
from LinuxQuestions.org on (#5NPH5)
Greetings all,

I found this code of grep to search for multiple patterns on same line in any order.

Code:grep -P '(?=.*?word1)(?=.*?word2)(?=.*?word3)^.*$' fooi test it and it works as is.
Code:grep -P '(?=.*?brown)(?=.*?red)(?=.*?yellow)^.*$' sample.txt
the brown fox jumped over the red and yellow fence
the yellow bird flew over the brown and red cage
Then, I tried to use variables as patterns but it didn't work. here is my script.

Code:#!/bin/bash

a=brown
b=red
c=yellow

grep -P '(?=.*?"$a")(?=.*?"$b")(?=.*?"$c")^.*$' sample.txtWhen I run it, no results appear.

Can grep use vaiables as patterns?

Thankslatest?d=yIl2AUoC8zA latest?i=J5N3BrIvK3c:UPtIkVIe4Ew:F7zBnMy latest?i=J5N3BrIvK3c:UPtIkVIe4Ew:V_sGLiP latest?d=qj6IDK7rITs latest?i=J5N3BrIvK3c:UPtIkVIe4Ew:gIN9vFwJ5N3BrIvK3c
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments