Interview Prep/Shell Scripting/Arguments, I/O, here-docs, process substitution and subshells

What is a here-document, and what is the difference between `<<EOF`, `<<'EOF'` and `<<-EOF`?

BeginnerConceptArguments, I/O, here-docs, process substitution and subshells
Answer

A here-doc feeds the following lines to a command's stdin until the delimiter line.

cat > /etc/app/app.env &lt;&lt;EOF           # unquoted: $vars and $(cmds) expand
APP_ENV=$env
BUILD=$(git rev-parse --short HEAD)
EOF

cat > deploy-remote.sh &lt;&lt;'EOF'         # quoted: literal, nothing expands
echo "running on $(hostname) as $USER"
EOF

if true; then
	cat &lt;&lt;-EOF                        # &lt;&lt;- strips leading TABS (not spaces)
	indented with tabs
	EOF
fi

The closing delimiter must be alone on its line with no trailing spaces. Use &lt;&lt;'EOF' when generating scripts or configs containing $ that must survive.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arguments, I/O, here-docs, process substitution and subshells