3

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.)

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

1 Answers1

9

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`"
Zeta
  • 106