Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash - nested EOF

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.

like image 811
Kingvinst Avatar asked Jan 26 '23 17:01

Kingvinst


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
like image 124
robsiemb Avatar answered Jan 28 '23 07:01

robsiemb