Score:0

bash heredoc and code block

it flag

Script has to log everyting in it to output and to log file

Like this it works OK:

#!/usr/bin/env bash

some_command_1
((
echo "Some text (in parenthness too)"
echo "Another text without them"
) &2>1 )|tee log.txt
some_command_2
exit 0

It outputs to stdin/stderr and to log file as expected.

But this script:

#!/usr/bin/env bash

some_command_1
((
cat <<EOF
Some text (in parenthness too)
Another text without them

EOF
) &2>1 )|tee log.txt
some_command_2
exit 0

Produces an error:

warning: here-document at line 9 delimited by end-of-file (wanted `EOF')

Another text without them: command not found

./tmp.sh: line 11: EOF: command not found

ESCaping like this "(" does nothing.

Why?

Score:0
tn flag

I believe that the problem is in the line containing ((. This double bracket has a special meaning in bash. Your script works for me in Linux if I separate the parentheses into separate lines.

#!/usr/bin/env bash

echo Start
(
(
cat <<XEOF
Some text (in parenthness too)
Another text without them

XEOF

) &2>1 )|tee log.txt
echo Stop
exit 0
Someone avatar
it flag
Yes, this way "heredoc with parenthness" works as expected. Even if write'em on one line, but separate by space, like this: "( (" Thanks!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.