What Are Here Documents in Bash?
Here documents (often abbreviated as heredocs) are a feature in Bash that allows you to create multi-line strings or input directly within your scripts. They provide a convenient way to pass a block of text to commands or scripts without needing to create temporary files. This guide will explain the purpose of here documents, their syntax, and provide examples of how to use them effectively.
1. Purpose of Here Documents
Here documents are useful for:
- Providing multi-line input to commands that expect standard input.
- Embedding large blocks of text directly in scripts, such as configuration files, SQL queries, or documentation.
- Improving readability by avoiding the need for multiple echo statements or temporary files.
2. Basic Syntax of Here Documents
The basic syntax for a here document is as follows:
command <<eof line 1 line 2 ...></eof>
In this syntax:
command
is the command that will receive the input.EOF
is a delimiter that marks the beginning and end of the here document. You can use any string as a delimiter, butEOF
is commonly used.- The lines between the two
EOF
markers are treated as input to the command.
3. Example of Using Here Documents
Let’s look at a simple example of using a here document to provide input to the cat
command.
Example of a Here Document with cat
cat <<eof this is line 1. this is line 2. this is line 3.></eof>
In this example:
- The
cat
command receives the multi-line input from the here document. - The output will be:
This is line 1.
This is line 2.
This is line 3.
4. Using Here Documents with Variables
You can also include variables within a here document. The variables will be expanded to their values when the here document is processed.
Example of Using Variables in a Here Document
name="John"
cat <<eof hello, $name! welcome to the bash scripting tutorial.></eof>
In this example:
- The variable
name
is defined and then used within the here document. - The output will be:
Hello, John!
Welcome to the Bash scripting tutorial.
5. Using Here Documents with Commands
Here documents can be used with various commands, including sql
commands, configuration files, and more.
Example of Using Here Document with a SQL Command
mysql -u username -p <<eof create database test_db; use test_db; create table users (id int, name varchar(100));></eof>
In this example:
- The here document provides a series of SQL commands to the
mysql
command. - This allows you to execute multiple SQL statements in one go without needing to create a separate SQL file.
6. Using Here Documents with Indentation
If you want to maintain indentation in your here document, you can use a feature called "indented here documents." This allows you to indent the lines of the here document while still keeping the content intact.
Example of Indented Here Document
cat <<-EOF
This is an indented line 1.
This is an indented line 2.
This is an indented line 3.
EOF
In this example:
- The
-EOF
syntax allows for leading tabs to be ignored, making it easier to maintain code readability. - The output will be:
This is an indented line 1.
This is an indented line 2.
This is an indented line 3.
7. Conclusion
Here documents are a powerful feature in Bash that simplify the process of providing multi-line input to commands. They enhance script readability and allow for the embedding of large blocks of text directly within scripts. By understanding how to use here documents effectively, you can streamline your Bash scripting and improve the overall structure of your code.