I am using this command
num1=2.2
num2=4.5
result=$(awk 'BEGIN{print ($num2>$num1)?1:0}')
This always returns 0. Whether num2>numl or num1>num2
But when I put the actual numbers as such
result=$(awk 'BEGIN{print (4.5>2.2)?1:0}')
I would get a return value of 1. Which is correct.
What can I do to make this work?
The reason it fails when you use variables is because the
awkscript enclosed by single quotes is evaluated byawkand notbash: so if you’d like to pass variables you are using frombashtoawk, you’ll have to specify it with the-voption as follows:Note that program variables used inside the
awkscript must not be prefixed with$