0

I have an 12.sh file with the below code

cat > /home/12.txt << "EOF"
[Unit]
${OUTPUT}
EOF

OUTPUT is an environment variable i want to import its value into the 12.txt file when i execute 12.sh file

Any one can tell how to do , Thanks in advance

Dsyd Shi
  • 3
  • 3
  • 1
    You have to drop double-quotes around `<< EOF`! – F. Hauri - Give Up GitHub Jan 21 '21 at 11:17
  • Does this answer your question? [How can I write a heredoc to a file in Bash script?](https://stackoverflow.com/questions/2953081/how-can-i-write-a-heredoc-to-a-file-in-bash-script) – F. Hauri - Give Up GitHub Jan 21 '21 at 11:18
  • See [Using variables inside a bash heredoc](https://stackoverflow.com/questions/4937792/using-variables-inside-a-bash-heredoc) – F. Hauri - Give Up GitHub Jan 21 '21 at 11:19
  • @DsydShi : From the man page, section _Here Documents_ : _If **any part of word is quoted**, the delimiter is the result of quote removal on word, and **the lines in the here-document are not expanded**._ . IMO, it's counter-intuitive, but it's defined in that way. – user1934428 Jan 21 '21 at 11:52
  • @DsydShi : Just noticed that you are not using bash. But for POSIX shell is the same. [Here](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_07_04) is the official explanation for your case. – user1934428 Jan 21 '21 at 11:57

1 Answers1

0

You don't need to use cat, just echo the data required are redirect the output to the file

echo -e "[Unit]\n$OUTPUT" > /home/12.txt
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18