Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

cat to combine heredoc and file

I’m trying to and failing to understand why this doesn’t work. I’m trying to use cat to combine an existing file and a heredoc into one file:

# test.txt
Hi there
$ cat test.txt <<END > /tmp/combined.txt
This is heredoc
END

I would have expected this to function in essence like:

cat file.txt file1.txt > /tmp/combined.txt

But what I actually see is just the contents of test.txt:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#/tmp/combined.txt
Hi there

If I do just the heredoc it works. If i do just the file it works obviously. What is it about combining them that makes cat ignore the heredoc?

(I know I can use tee and a variety of other approaches to do this but I want to fill in my understanding of cat, redirection etc)

>Solution :

cat echos the contents of its input filename arguments or standard input if none are given, but not both. However, if one of the arguments is -, that one is treated as reading from standard input:

$ cat test.txt - <<EOF
This is heredoc
EOF
Hi there
This is heredoc
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading