echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $(cat) }"
The above syntax works fine with the calculated result '1337'.
echo '((3+(2^3)) * 34^2 / 9)-75.89' | awk "BEGIN{ print $* }"
But the above syntax doesn't work, though there's no error.
Plz advise.
The
$(command)
syntax will return the output ofcommand
. Here, you are using the very simplecat
program whose only job is to copy everything from standard input (stdin) into standard output (stdout). Since you are running theawk
script inside double quotes, the$(cat)
is expanded by the shell before theawk
script is run, so it reads theecho
output into its stdin and duly copies it to its stdout. This is then passed to theawk
script. You can see this in action withset -x
:So,
awk
is actually runningBEGIN{ print ((3+(2^3)) * 34^2 / 9)-75.89 }'
which returns 1337.Now, the
$*
is a special shell variable that expands to all the positional parameters given to a shell script (seeman bash
):However, this variable is empty here. Therefore, the
awk
script becomes:The
$*
expands to an empty string, andawk
is told to print an empty string, and this is why you get no output.You might want to just use
bc
instead: