Solved Need help with heredoc

I've been wrestling with this tiny problem all, but can't come up with a solution...

I'm trying to come up with a heredoc which will create these few lines of code:-
Bash:
str="$(cat <<EOS)"                                                                                                                                           
abc                                                                                                                                                         
xyz                                                                                                                                                         
EOS                                                                                                                                                         
                                                                                                                                                            
echo $str

This simply outputs abc xyz.

What I'm trying to do is create a heredoc which outputs the above code but can't figure out what needs to be escaped.

Starting from this, I guess I need to escape '$', but not sure about '"', '(', ')' or '<<'
Bash:
cat <<EOF                                                                                                                                                   
str="$(cat <<EOS)"                                                                                                                                           
abc                                                                                                                                                         
xyz                                                                                                                                                         
EOS                                                                                                                                                         
                                                                                                                                                            
echo $str                                                                                                                                                   
EOF

Any suggestions appreciated.
 
If you want to put data into a shell variable there is no need for a heredoc:​
Bash:
str='abc
xyz
'
Now ${str} contains abc, <newline>, xyz, <newline>.​
This simply outputs abc xyz.
To ensure that a command receives an argument as one argument, you need to wrap it into a string literal:​
Bash:
echo "${str}"
 
I think you misunderstand what I'm trying to do....

The number of elements in the line will be variable. I'm using a heredoc to build a script for later execution. What I'm getting at is a way of building this command:-

Bash:
str="$(cat <<EOS)"                                                                                                                                           
abc                                                                                                                                                         
xyz                                                                                                                                                         
EOS                                                                                                                                                         
                                                                                                                                                            
echo $str

via a shell script. The final command will be longer, but I want to get this sorted first.
 
you should only have to escape the $ in a bash heredoc (don't worry about the other chars), but I'm confused about the $(cat <<EOS) syntax that your script publishes, it doesn't do anything, maybe at that point you would want to follow Kai Burghardts advice on capture multiple lines into a variable?

Code:
#!/bin/sh

cat <<EOF
str="\$(cat <<EOS)"
abc
xyz
EOS

echo \$str
EOF
 
you should only have to escape the $ in a bash heredoc (don't worry about the other chars), but I'm confused about the $(cat <<EOS) syntax that your script publishes, it doesn't do anything, maybe at that point you would want to follow Kai Burghardts advice on capture multiple lines into a variable?

According to


all these are special characters:-

| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>

Code:
#!/bin/sh

cat <<EOF
str="\$(cat <<EOS)"
abc
xyz
EOS

echo \$str
EOF

This produces:-
Code:
str="$(cat <<EOS)"                                                                                                                                                             
abc                                                                                                                                                                           
xyz                                                                                                                                                                           
EOS                                                                                                                                                                           
                                                                                                                                                                              
echo $str
 
the desired output isn't correct in regards to $ shellcheck -s bash ba.sh but I assume it should be a bash script.

Anyway:

Code:
cat <<FOO
str="\$(cat <<EOS)"
abc
xyz
EOS

echo "\$str"
FOO
 
According to


all these are special characters:-

| & ; < > ( ) $ ` \ " ' <space> <tab> <newline>



This produces:-
Code:
str="$(cat <<EOS)"                                                                                                                                                            
abc                                                                                                                                                                          
xyz                                                                                                                                                                          
EOS                                                                                                                                                                          
                                                                                                                                                                             
echo $str

So... this is the right answer for you, right?
 
Quote EOF as below.
cat <<'EOF'
str="$(cat <<EOS)"
abc
xyz
EOS

echo $str
EOF
I see that bash manpage doesn't point this out (not that I read it carefully!). Stick to what works in /bin/sh even if you use bash as usually the original Bell Labs manpages are more complete. From "man sh":

The following redirection is often called a “here-document”.

[n]<< delimiter
here-doc-text
...
delimiter

All the text on successive lines up to the delimiter is saved away and
made available to the command on standard input, or file descriptor n if
it is specified. If the delimiter as specified on the initial line is
quoted, then the here-doc-text is treated literally, otherwise the text
is subjected to parameter expansion,
 
Thanks to all that made suggestions. I now have a code snippet which works for me and does exactly what I was hoping for:-

Bash:
cat <<EOF >/test.sh                       
str="pkg -r /mnt install -y \$(cat <<EOS)"
drm-kmod                                 
xorg                                     
lxde-meta                                 
EOS                                       
                                          
\$str                                     
EOF                                       
                                          
cat /test.sh
 
Back
Top