-1
while read wholeline
do
  echo ${wholeline} --outputs John Jones
  #grab all the lines that begin with these values
  #grep '^John Jones' workfile2   --works, prints contents of workfile2 where the 1st matches
  grep '^${wholeline}' workfile2  # --does not work. why? it should be the same result.
done < workfile1

workfile1 contains the value John Jones at the beginning of the line in the file.

workfile2 contains the value John Jones at the beginning of the line in the file.

How can I change that second grep statement so that it works? It is picking up nothing.

jasonwryan
  • 73,126

1 Answers1

1

You are using single quotes, try using double quotes. For the shell to expand variables, you need to use double quotes.

while read wholeline
do
  echo ${wholeline} --outputs John Jones
  #grab all the lines that begin with these values
  #grep '^John Jones' workfile2   # --works, prints contents of workfile2
                                  # where the 1st matches
  grep "^${wholeline}" workfile2  # --does work now, since shell 
                                  # expands ${wholeline} in double quotes
done < workfile1
peterph
  • 30,838
rahul
  • 1,181
  • Thank you so much. I needed double quotes indeed. Great site. I will learn how to format my questions properly for next time. – claghorn Feb 08 '15 at 05:16
  • +1, but please do read the markdown help to properly format your answers – peterph Feb 08 '15 at 10:21