0

This question already has an answer here:

How come

 echo `echo "foo"`

works but

 echo `awk '{ print "foo" }'`

doesn't?

marked as duplicate by muru, Jeff Schaller, Toby Speight, roaima, G-Man May 16 '18 at 3:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    Awk is waiting for input. – muru May 15 '18 at 5:28
5

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.

  • 2
    All awks 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 6:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.