Why does this work:
This
var=hello
myvar=`echo hello hi | awk "{ if (\\\$1 == \"$var\" ) print \\\$2; }"`
echo $myvar
gives
hi
But this does not?
This
var=hello
echo hello hi | awk "{ if (\\\$1 == \"$var\" ) print \\\$2; }"
gives
awk: cmd. line:1: Unexpected token
I am using
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)
on
Linux 2.6.32-34-generic-pae #77-Ubuntu SMP Tue Sep 13 21:16:18 UTC 2011 i686 GNU/Linux
If your awk is like mine, it will tell you where it fails:
furthermore, if you replace
awkbyechoyou’ll see clearly why it failsthere are extra ‘\’ (backslashes) in the resulting command. This is because you removed the backquotes.
So the solutions is to remove a pair of \’s