Bash interprets $1, $2 of an awk script when used in HERE-TAG ...

For the following code:

awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1'  shadow2 shadow1 > shadow3 

I get syntex error as below:

awk: NR==FNR{a[]=;next}a[]{=a[]}1
awk:           ^ syntax error
awk: fatal: invalid subscript expression

On investigation, I noticed that when I create a bash script using the HERE-TAG(from within another bash script) ; in the following manner:

#!/usr/bin/env bash
# Bash_script001.sh

   echo " Inside Bash_script001.sh";
   ...
   ...
   ...

cat>>"Bash_script002.sh"<<EOF
#!/usr/bin/env bash
# Bash_script002.sh

   echo " Inside Bash_script002.sh";
   ...

   awk -F":" -v OFS=":" 'NR==FNR{a[$1]=$2;next}a[$1]{$2=a[$1]}1' shadow2 shadow1 > shadow3;
   ...

   ...

EOF

   ....
   echo " Again inside Bash_script001.sh processing rest of Bash_script001.sh ";

Now when the 'Bash_script002.sh' gets created and as it contains the awk script which inturns contains variables $1, $2 as array a[] indexes; the resultant 'Bash_script002.sh' contains only the following:

awk -F":" -v OFS=":" 'NR==FNR{a[]=;next}a[]{=a[]}1' shadow2 shadow1 > shadow3;

Thats why I'm receiving the syntex error.

Will you please suggest, what can be done to avoid getting the above erronious 'awk' statement in the resultant 'Bash_script002.sh' ?

Thanks in anticiption.

'man ksh' yeilds:

     << [-]word
           The shell input is read up to a line that is the  same
           as word, or to an EOF. No parameter substitution, com-
           mand substitution, or file  name  generation  is  per-
           formed  on  word.  The  resulting  document,  called a
           here-document, becomes  the  standard  input.  If  any
           character  of  word  is  quoted,  no interpretation is
           placed upon the characters of the document. Otherwise,
           parameter  and command substitution occur, \NEWLINE is
           ignored, and \ must be used to quote the characters \,
           $,  `,  and  the  first  character  of  word.  If - is
           appended to <<, then all  leading  tabs  are  stripped
           from word and from the document.
cat>>"Bash_script002.sh"<<'EOF'

It is unclear for me what you want to achieve and how the positional parameters are set but perhaps is this what you are looking for:

awk -F":" -v OFS=":" 'NR==FNR{a['$1']='$2';next}a['$1']{'$2'=a['$1']}1'  shadow2 shadow1 > shadow3

I'm extremely thankful to you.
This worked.

---------- Post updated at 02:47 AM ---------- Previous update was at 02:27 AM ----------

Thanks jlliagre; I got this working by providing single quotes to EOF itself.

[quote=vgersh99;302353158]
'man ksh' yeilds:

     << [-]word
           The shell input is read up to a line that is the  same
           as word, or to an EOF. No parameter substitution, com-
           mand substitution, or file  name  generation  is  per-
           formed  on  word.  The  resulting  document,  called a
           here-document, becomes  the  standard  input.  If  any
           character  of  word  is  quoted,  no interpretation is
           placed upon the characters of the document. Otherwise,
           parameter  and command substitution occur, \NEWLINE is
           ignored, and \ must be used to quote the characters \,
           $,  `,  and  the  first  character  of  word.  If - is
           appended to <<, then all  leading  tabs  are  stripped
           from word and from the document.

Cool. I didn't know that, my suggestion first was to escape $-signs with back slahes, e.g. \$1, but your solution is much more handsome