I'm trying to prefix and append blocks of text to a list of files. Here is where I am so far. The sticking point is the sed -i "1i \$prefix" "$file" && line. sed won't substitute the value of prefix in. I tried to follow various threads about this on unix.sx and a couple of other places, but just got a headache.
Can someone tell me how to fix this? If it is too complicated I'm Ok with using something else instead of sed.
This is closely related to How do I append text to the beginning and end of multiple text files in Bash?, which is where I got the code below from. The difference is that the question does not cover expanding a variable, and also in my case the strings in question are multiline.
filelist=(foo.tex bar.tex)
prefix='\documentclass[12pt]{article}
\usepackage{myarticle}
%\xpretocmd{\opening}{\insertname}{}{}
\begin{document}
%\insertname
\begin{verbatim}'
suffix='\end{verbatim}
\end{document}'
for file in "${filelist[@]}"; do
sed -i "1i \$prefix" "$file" &&
echo "$suffix" >> "$file"
done