-1

This question already has an answer here:

Just wondering if anyone can explain why this works:

echo `cat <<EOF
  {"branch":"foo","value":"bar"}
EOF`

but this doesn't:

echo <<EOF
  {"branch":"foo","value":"bar"}
EOF

(the second snippet doesn't echo anything to stdout.)

marked as duplicate by muru, msp9011, schily, Archemar, Jeff Schaller Aug 14 '18 at 11:01

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.

4

Because echo never reads from stdIn. Try this:

echo foo bar | echo

however cat does, if and only if it haves no files in it's args:

echo foo bar | cat

your first example is inserting the result of ... as the argument of echo. your second example is feeding the HEREDOC to echo, and thus ignored.

also, your first example is not protecting the argument to echo, which means the shell will expand it AGAIN after passing it to echo.

Consider a harmuful character as bang, This does not work:

echo `cat <<EOF
  {"branch":"foo","value":"bar!"}
EOF`

This does:

echo "`cat <<EOF
  {"branch":"foo","value":"bar!"}
EOF`"

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