変数 "grep"ファイルを使用するスクリプト

変数
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

workfile1John Jonesファイルの行の先頭にある値を含みます。

workfile2John Jonesファイルの行の先頭にある値を含みます。

2番目の文をどのように変更して機能さgrepせることができますか?何も得られませんでした。

ベストアンサー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 work now, since shell 
                                  # expands ${wholeline} in double quotes
done < workfile1

おすすめ記事