Home > Enterprise >  How to use a heredoc inside a command substitution?
How to use a heredoc inside a command substitution?

Time:03-08

Trying to beautify following command so I don’t have to use \n to signal like breaks.

$ http POST http://localhost/sendmail \
subject="Test" \
body=$'foo\nbar!' \
preview=true

Using a heredoc (the <<EOF syntax), I could copy paste message within boundaries resulting in predictable UX… tried following, but command fails.

$ http POST http://localhost/sendmail \
subject="Test" \
body="$(cat << "EOF"
foo
bar!
EOF)" \
preview=true

What am I missing? Thanks for helping out!

CodePudding user response:

The line has to be EOF and exactly EOF nothing else.

$ http POST http://localhost/sendmail \
subject="Test" \
body=$(cat << "EOF"
foo
bar
EOF
) \
preview=true

Note that your $(...) undergoes word splitting and filename expansion. You might have intended to put it inside "$(...)" double quotes to prevent these expansions.

  •  Tags:  
  • bash
  • Related