How come
echo `echo "foo"`
works but
echo `awk '{ print "foo" }'`
doesn't?
How come
echo `echo "foo"`
works but
echo `awk '{ print "foo" }'`
doesn't?
The awk
program will wait for input and, for each line of input, print the word foo
. That is what the awk
program { print "foo" }
does.
In contrast, echo
, in the first command substitution, does not wait for input.
Would you want an awk
program to just print something, without any input, do the output in a BEGIN
block:
awk 'BEGIN { print "foo" }'
The BEGIN
block is executed before reading the first line of input, and since there are no other blocks in the script, and no input file, it will then exit.
Also, never write code like echo $( ... )
or echo ` ... `
, just use the code inside the command substitution instead.
awk
s I have also return immediately with just awk 'BEGIN { print "foo" }'
, as does just awk ""
, i.e. if it doesn't have any rules apart from BEGIN
, it doesn't wait for input.
– ilkkachu
May 15 '18 at 06:56