sunknudsen
sunknudsen

Reputation: 7310

How to use a heredoc inside a command substitution?

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!

Upvotes: 1

Views: 993

Answers (1)

KamilCuk
KamilCuk

Reputation: 141883

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.

Upvotes: 2

Related Questions