Bash HereDoc Tutorial With Examples

If you're a Linux or Unix user, you're probably familiar with Bash, the command-line shell that's commonly used on these systems. Bash has a lot of powerful features that can make working on the command line much more efficient, and one of those features is called a HereDoc. In this tutorial, we'll explain what a HereDoc is, how it works, and give you some examples of how you can use it in your Bash scripts.

What is a HereDoc?

A HereDoc, short for "Here Document," is a way to include a block of text within a Bash script. This block of text can contain anything you want, including commands, variables, and other special characters. HereDocs are useful because they allow you to include large blocks of text without having to worry about escaping special characters or dealing with complicated quoting rules.

Here's an example of a HereDoc in action

cat << EOF
This is a HereDoc example.
It can include multiple lines of text.
EOF

In this example, we're using the cat command to output a block of text to the terminal. The << EOF syntax tells Bash to start a HereDoc block, and EOF at the end tells Bash where the block ends. Anything between those two markers is included in the HereDoc.

The HereDoc block can contain any text you want, including variables, commands, and other special characters. Here's an example that demonstrates how to use a HereDoc to store a SQL query in a variable

query=$(cat << EOF
   SELECT *
   FROM users
   WHERE username='john.doe';
   EOF
)

echo "$query"

In this example, we're using a HereDoc to store a SQL query in the query variable. We're then using the echo command to output the contents of the variable to the terminal.

HereDoc Syntax

The basic syntax for a HereDoc is as follows

command << DELIMITER
text
DELIMITER

In this syntax, command is the command that will receive the HereDoc as input, and text is the block of text that will be included in the HereDoc. The DELIMITER marker tells Bash where the HereDoc ends.

The HereDoc syntax is very flexible and can be used in a variety of ways:

You can use a HereDoc to provide input to a command

command << EOF
input
EOF

You can use a HereDoc to assign a block of text to a variable

variable=$(cat << EOF
   text
   EOF
)

You can use a HereDoc to create a file with a block of text

cat << EOF > file.txt
text
EOF

You can use a HereDoc to append a block of text to a file

cat << EOF >> file.txt
text
EOF

HereDoc Examples

Let's take a look at some real-world examples of how HereDocs can be used in Bash scripts.

Example 1: Creating a Configuration File

Suppose you're writing a script that needs to read settings from a configuration file. Instead of creating the file manually, you can use a HereDoc to generate the file from within your script

cat << EOF > config.ini
[server]
host = example.com
port = 80

[database]
name = mydatabase
user = myuser
password = mypassword
EOF

In this example, we're using a HereDoc to generate a configuration file called config.ini. The file contains several sections, including a "server" section and a "database" section. Each section contains key-value pairs that define settings for the script.

Example 2: Running a SQL Script

Suppose you have a SQL script that you need to run on a remote server. You can use a HereDoc to run the script remotely

ssh user@example.com << EOF
mysql -u myuser -pmypassword mydatabase << 'SQL'
SELECT * FROM users;
INSERT INTO logs (message) VALUES ('Script executed');
SQL
EOF

In this example, we're using a HereDoc to run SQL commands on a remote server. We're using the ssh command to connect to the server, and the HereDoc block contains the MySQL commands to execute.

Example 3: Generating HTML Output

You can use a HereDoc to generate HTML code dynamically

title="My Dynamic Website"
content="Welcome to our site!"

cat << EOF > index.html
<!DOCTYPE html>
<html>
<head>
   <title>$title</title>
</head>
<body>
   <h1>$content</h1>
   <p>This page was generated on $(date).</p>
</body>
</html>
EOF

In this example, we're using a HereDoc to generate an HTML file with dynamic content. The variables $title and content are expanded within the HereDoc, and the $(date) command substitution adds the current date.

HereDoc Variations

Quoted HereDoc (No Variable Expansion)

To prevent variable expansion and command substitution, quote the delimiter

cat << 'EOF'
This $variable will not be expanded.
The $(date) command will not be executed.
EOF

Indented HereDoc

Use <<- to remove leading tabs (not spaces) from each line

if true; then
    cat <<- EOF
	This line is indented with a tab
	So is this one
	EOF
fi

Best Practices

  • Use descriptive delimiters Instead of EOF, use meaningful names like SQL, HTML, or CONFIG.

  • Quote delimiters when needed Use single quotes around the delimiter to prevent variable expansion when literal text is desired.

  • Mind the indentation Use <<- and tabs (not spaces) when you need to indent the HereDoc within your script structure.

  • Avoid mixing tabs and spaces This can cause formatting issues, especially with commands that expect specific formatting.

Conclusion

HereDocs are a powerful Bash feature that simplifies working with multi-line text blocks in scripts. They eliminate the need for complex quoting and escaping while supporting variable expansion and command substitution. Whether you're generating configuration files, creating HTML output, or handling SQL queries, HereDocs provide a clean and readable solution for managing text content in your Bash scripts.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements