How do I echo a lot of content in a bash script?

I would like to do

echo "[this thing]"


This thing is

\documentclass{article}
\usepackage{rotating}
\usepackage{pdfpages}
\usepackage{verbatim}
\usepackage{amsmath, amsfonts, amssymb, textcomp, mathtools, xparse}
\usepackage[T4, OT1]{fontenc}
\usepackage{graphicx}
\graphicspath{{/Users/Masi/Dropbox/Physiology/images/}}
% Animations cannot be included here
\usepackage{newunicodechar}
\usepackage{multirow}
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}
\DeclarePairedDelimiter{\norm}{\lVert}{\rVert}
\usepackage{color}
\usepackage{hyperref}
\usepackage{media9} % animations swf
\usepackage{Tabbing}
\usepackage{doi, natbib}
\hypersetup{
citecolor=blue,
allcolors=blue
}
\usepackage[affil-it]{authblk}
\usepackage{import}
\usepackage{color}
\usepackage[normalem]{ulem}
\usepackage{titling} % Two titles in one document
\DeclarePairedDelimiter{\abs}{\lvert}{\rvert}

\usepackage[framemethod=tikz]{mdframed}

\mdfdefinestyle{ans}{
linecolor=cyan,
backgroundcolor=yellow!20,
frametitlebackgroundcolor=green!40,
frametitlerule=true
}
\newcounter{question}[section]%
\setcounter{question}{0}

\newenvironment{question}[1]{%
\refstepcounter{question}%
\begin{mdframed}[style=ans,frametitle={Question: #1}]
}{%
\end{mdframed}%
}%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%% Smaller things

\newtheorem{case}{Case logic}
\mdfdefinestyle{que}{
linecolor=cyan,
backgroundcolor=cyan!20,
}
\surroundwithmdframed[style=que]{case}

\newtheorem{sidenote}{Sidenote}
\mdfdefinestyle{que}{
linecolor=cyan,
backgroundcolor=cyan!20,
}
\surroundwithmdframed[style=que]{sidenote}

\newtheorem{citation}{Citation}
\mdfdefinestyle{que}{
linecolor=cyan,
backgroundcolor=yellow!20,
}
\surroundwithmdframed[style=que]{citation}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newtheorem{theorem}{Theorem}
\newtheorem{proposition}[theorem]{Proposition}
\newenvironment{definition}[1][Definition]{\begin{trivlist}
\item[\hskip \labelsep {\bfseries #1}] \emph}{\end{trivlist}}
\providecommand{\keywords}[1]{\textbf{Keywords:} #1}

%%%%%%%%%%%%%%%%%%%%%%%%% Counter Section %%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\def\@part[#1]#2{%
\ifnum \c@secnumdepth >\m@ne
\refstepcounter{part}%
\fi
{\parindent \z@ \raggedright
\interlinepenalty \@M
\normalfont
\LARGE \bfseries #2%
\markboth{}{}\par}%
\nobreak
\vskip 3ex
\makeatother
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


How can you echo such a big content in Bash file well?

Use a here document:

cat <<'EOF'
Data...
EOF


Note: it's better to quote the heredoc word (EOF) as above to avoid expansion if the data contains something like $foo or backslashes, unless you want expansion of course. Examples: $ cat <<EOF
$SHLVL \\ EOF  gives something like: 3 \  while $ cat <<'EOF'
$SHLVL \\ EOF  gives: $SHLVL \\

• This works! I think best solution here. Thank you for your answer! – Léo Léopold Hertz 준영 Oct 5 '14 at 16:00
• I have some problem with the approach of the latest edit: # cat <<'EOF' start_tex \\ EOF gives error where start_tex is a simple file with some variables. Can you give an example how to use this EOF approach with catting file. My working case is just this cat start_tex - without variables, however. – Léo Léopold Hertz 준영 Oct 5 '14 at 17:09
• @Masi please edit your question then and clarify. You don't mention a file anywhere in your question. Try and make your questions as specific as possible. Anyway, you're looking for cat start_tex > out.tex which is much simpler. – terdon Oct 5 '14 at 17:25
• @Masi If the data are in a file, just use cat file in the usual way. The here document solution is used when the data are in the script, in a similar way to echo "[this thing]" (as in the question). Make sure that EOF is on its own line. – vinc17 Oct 5 '14 at 17:27

The @vinc17 answer is correct but incomplete. The HEREDOC approach is great but it can't be used naively. See https://stackoverflow.com/a/11379627/763269 for more things to be aware of.

Embedded spaces and tabs are not preserved as yo'd think in a HEREDOC block. The HEREDOC marker (typically EOF) should be single-quoted to prevent shell expansion. A HEREDOC block containing another HEREDOC block (i.e. if you are generating a script) has many problems. All such problems can be avoided by base64-encoding the content of the HEREDOC block and expanding that.

• Can you also summarize your linked answer here shortly. – Léo Léopold Hertz 준영 Aug 14 '16 at 18:59
• Embedded spaces and tabs are not preserved as yo'd think in a HEREDOC block. The HEREDOC marker (typically EOF) should be single-quoted to prevent shell expansion. A HEREDOC block containing another HEREDOC block (i.e. if you are generating a script) has many problems. All such problems can be avoided by base64-encoding the content of the HEREDOC block and expanding that. – Chris Johnson Aug 15 '16 at 11:57
• Can you please give a code example where you compare your proposal with Vin27's proposal. – Léo Léopold Hertz 준영 Aug 15 '16 at 12:37

My latex is not so good, do you set any variables in the text? Otherwise I would put the latex in an extra template file and just use cat to print it. This would make it more maintainable in case you want to replace the template one day.

• Welcome to Unix & Linux! This is really a comment, not an answer. Answers should only be used to provide actual answers not to ask for clarification. If you spend some more time on the site, you will gain sufficient privileges to leave comments on posts. In the meantime, I have converted your answer to a comment. – terdon Oct 5 '14 at 17:22
• Can you add an example to your suggestion? It seems this solution is more abstract than it needs to be. – HalosGhost Oct 5 '14 at 17:32