0

I have a shell script where i want to print date which is inside a heredoc. Below is the code snippet.

echo "This is a text"
echo I want date to print inside heredoc <<EOF
var1=`date`
echo $var1
EOF
echo "Thats All"

Output of the above code is

I want date to print inside heredoc
Thats All

Its not printing the date. Even if i put any echo command its not printing. What can be wrong here.

NOTE: i tried adding the below in my code for heredoc ___EOF -- Did not work 'EOF' -- Did not work

Please suggest.

This is my actual code when i have to switch to a different user and then perform some operations over there

su - oracle << EOF
echo $(xyz file.prop) > replace.txt
var1=`cat replace.txt_orig`
var2=`cat replace.txt`

echo $var1
echo $var2
EOF

Here the Variable assignment and echo is not working.

1
  • Generally you should not be using backticks in new code. Instead, use $( ... ) for example var1=$(date). Secondly, double-quote your variables when you use them, so echo "$var1". In this particular situation it won't make any visible difference but it's an important habit to gain, so that when it does make a difference you don't get bitten
    – roaima
    Dec 14, 2021 at 23:18

2 Answers 2

3

Heredocs are read as STDIN unless a file descriptor is specified but echo does not read from stdin (or from any fd).

Additionally while command substitution and parameter expansion will work inside a heredoc (as long as WORD in << WORD isn't quoted), variable assignment will not work, nor will commands outside of command substitution.

I'm not even sure what you are ultimately trying to accomplish here and this seems like an XY problem but you can use cat instead to read your heredoc and it will work as expected

var1=$(date)
cat <<EOF
$var1
EOF

or simply:

cat <<EOF
$(date)
EOF
5
  • Hi @jesse_b As i mentioned that its just a code snippet, what i am actually trying to do is like below: ` su - oracle << ___EOS echo $(grep xyz file.prop) > replace.txt #putting the text files in a variables var1=cat replace.txt_orig ___EOS ` this seems to be ot working. Is there any alternate method. as i have to login as a oracle user and then do the required work?
    – Shubhro
    Dec 14, 2021 at 16:45
  • Even with the above code cat <<EOF var1=date`` echo $var1 EOF echo is not printing.
    – Shubhro
    Dec 14, 2021 at 17:11
  • @Shubhro yeah sorry you wouldn't want to put echo within the heredoc, and variable assignments wouldn't work within a heredoc either.
    – jesse_b
    Dec 14, 2021 at 17:16
  • @Shubhro please edit your question and include all this so we know what you actually need.
    – terdon
    Dec 14, 2021 at 17:38
  • 1
    It's not that echo doesn't print what it reads on stdin, echo does not read its stdin at all. See also Redirecting the content of a file to the command "echo" Dec 14, 2021 at 19:52
1

You don't need variable assignments or command substitutions in your here-document, because there is no need to read the result of the xyz command or the contents of the files into variables before producing them as output.

su - oracle <<'END_SU_SCRIPT'
xyz file.prop >replace.txt
cat replace.txt_orig
cat replace.txt
END_SU_SCRIPT

I chose to use a quoted here-document, because there are no expansions in it that the invoking shell needs to carry out.

The shell expanding stuff in your document was the issue that you had with your original here-document. Since the here-document was unquoted, the invoking shell would expand all expansions in it, as if it had been a double-quoted string. So the command substitution $(xyz file.prop) would have been expanded as would both substitutions around cat and the $var1 and $var2 variables, and all of this would have happened before even calling su.

1
  • It's possible the OP's echo $(xyz...) was a misguided attempt to join the lines of xyz's output with spaces. In which case, you'd want to use xyz file.prop | paste -sd ' ' - > replace.txt Dec 15, 2021 at 7:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .