← Back to Bash Course | Chapter 3: Control Flow | Lesson 13 of 14

Here Documents (<<EOF)

In this page:

  1. Here Documents (<<EOF)

Here Documents (<<EOF)

A here document lets you feed multiple lines of literal text to a command's standard input, starting with <<EOF and ending at a line containing just the matching delimiter (EOF by convention, but any word works). Variables inside are expanded unless the delimiter is quoted, like <<EOF. Here documents are commonly used to write multi-line text into a file without many separate echo calls.

Note: Quote the delimiter, <<EOF, to prevent variable expansion inside the here document.

Example: Here Documents (<<EOF)

bash
#!/bin/bash
name="World"

cat <<EOF
Hello, $name!
This is a multi-line
here document.
EOF

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.