12

I´m trying to create a file using

cat - << EOF > file.sh

But inside this, I want to write another EOF. Its hard to explain, so here an example:

cat - << EOF > file1.sh
echo first
cat - << EOF > file2.sh
echo second
EOF
echo again first
EOF

But of course, at line 5 it breaks. It does not create file1.sh with the content line 2-6, but with the content line 2-4.

1
  • 2
    Just use different delimiter strings; they don't have to be "EOF". Oct 17, 2019 at 17:31

1 Answer 1

19

Just use a different delimiter on the outer cat, "EOF" isn't special in any way to the shell:

cat - << REALEND > file1.sh
echo first
cat - << EOF > file2.sh
echo second
EOF
echo again first
REALEND

Results in this content in file1.sh

echo first
cat - << EOF > file2.sh
echo second
EOF
echo again first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.