0

I have need to define a variable in a bash script in a grep commad as below

#!/bin/bash
DATA=502
DataGet=501
for i in  1 2 44 45
do
GET=$(echo "$i" "$DATA" | awk '{print $1*$2}')
rm $GET.dat
grep '   501   ' -A"$GET"  case.dat | head -n "$GET" | tail -n "$DataGet" > $i.dat
done

Instead of the seond last line, I want to have

grep ' "$DataGet" ' -A"$GET" case.dar | head -n "$GET" | tail -n "$DataGet" > $i.dat

but it does not print anything.

1

1 Answer 1

3

You should use double quotes to evaluate the variable.

~ > b=123
~ > echo '$b'
$b
~ > echo "$b"
123

So change your first string to this:

grep " \"$DataGet\" " -A "$GET" ...

Not the answer you're looking for? Browse other questions tagged or ask your own question.