bash - nested EOF

Viewed 2964

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 Answers

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
Related