What is a heredoc (<<EOF) in bash and when is it used?

Answer

A heredoc is a multi-line string literal in bash that feeds input to a command until a delimiter line is reached. Syntax: command <<EOF\nline 1\nline 2\nEOF. Variables and command substitutions are expanded inside the heredoc by default. To suppress expansion (treat the content as a literal string), quote the delimiter: <<'EOF'. Use <<-EOF (with a dash) to allow indented delimiter tabs (but not spaces). Common uses: writing multi-line config files in scripts (cat > /etc/nginx/site.conf <<EOF), passing multi-line SQL to mysql, multi-line SSH commands, and embedding JSON payloads in curl calls. Heredocs make scripts far more readable than many escaped echo statements.