The easiest way to describe the problem is to demonstrate it.
The goal is to store a command that upon execution would properly display the content of a variable CONTENT, including the line breaks.
If we are to add -x
to the #!
it would be clear the \n
gets swallowed during the interpolation $display_cmd
during execution even though it seems to be assigned properly. This in turn means that using printf
instead of echo
has the same result.
I have a workaround using cat and a HERE doc, but I would strongly prefer not to use it. The reason being that with HERE doc the EOF marker needs to start on the first column, which at some point someone is going to decide to make align with the rest of the code breaking the workaround.
Is here a way to accomplish what I want without using a HERE doc?
#!/bin/bash
CONTENT=$(cat << EOF
Untrusted first line
Untrusted second line
EOF
)
# This will work
echo "CONTENT variable will print on two different lines"
echo
echo -e "$CONTENT"
# This won't work
# Create a command
echo "And this will display content on a single line:"
echo
display_cmd="echo -e \"${CONTENT}\""
$display_cmd