10

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.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Kingvinst
  • 149
  • 1
  • 7

1 Answers1

16

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
robsiemb
  • 6,157
  • 7
  • 32
  • 46