Why does echo <<EOF ... EOF not work

15,062

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`"
Share:
15,062

Related videos on Youtube

Alexander Mills
Author by

Alexander Mills

Updated on September 18, 2022

Comments

  • Alexander Mills
    Alexander Mills over 1 year

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