Skip to content

Click on each book below to review & buy on Amazon.

As an Amazon Associate, I earn from qualifying purchases.


CompTIA Linux+ XK0-005 - 3.1 - Shell Script Elements - Here Documents

In shell scripting, a here document is a convenient way to include a block of text or data within a script. It allows you to provide input to commands or specify a portion of a script without the need for external files. Here documents are especially useful when you want to embed multiline data directly in your script or pass input interactively. In this guide, we will explore the concept of here documents and learn how to use them effectively in shell scripts.

Using Here Documents

To use a here document, you start with the << operator followed by a delimiter of your choice. The delimiter marks the beginning and end of the block of text. You can choose any word as the delimiter, as long as it doesn't conflict with other syntax within your script.

Here's the basic syntax:

command << delimiter
    text
delimiter

The command can be any command that accepts input from the standard input (stdin). The text is the block of data you want to provide as input.

Benefits of Here Documents

Using here documents in shell scripting offers several benefits:

  • In-line Data: With here documents, you can include data directly within your script without the need for external files. This simplifies the script and makes it more self-contained.

  • Multiline Input: Here documents allow you to specify multiline input easily. You can include line breaks and special characters within the block of text without any special formatting.

  • Interactive Input: Here documents enable you to provide input interactively within your script. This is particularly useful when you want to script a series of user prompts or simulate user interaction.

  • Code Readability: By embedding data directly in your script using here documents, you enhance the readability of your code. It's easier to understand and maintain the script when the data is included within the context.

Examples Using Here Documents

Let's look at two examples to understand the usage of here documents:

Displaying Content With cat

#!/bin/bash

# Here document example

cat << EOF
Hello,
Welcome to the script!
This is a multiline message.
EOF

In this example, the cat command is used to display the content of the here document. The << EOF indicates the start of the here document, and EOF marks the end. The text between the delimiters is displayed as the output of the cat command.

Searching Through Multiline Data With grep

#!/bin/bash

# Here document example with command input

read -p "Enter your name: " name

grep "$name" << END
John Doe
Jane Smith
Mike Johnson
END

In this example, we prompt the user to enter their name using the read command. The entered name is stored in the name variable. We then use a here document to pass the name as input to the grep command. The text block between the << END and END delimiters represents the input that will be searched by grep. The grep command will search for the entered name within the text block and display any matching lines.

Conclusion

Here documents are a powerful feature in shell scripting that allow you to include in-line data, provide multiline input, and interactively pass input to commands. By understanding how to use here documents effectively, you can simplify your scripts, make them more readable, and handle various scenarios where including data directly within the script is advantageous.


Support DTV Linux

Click on each book below to review & buy on Amazon. As an Amazon Associate, I earn from qualifying purchases.

NordVPN ®: Elevate your online privacy and security. Grab our Special Offer to safeguard your data on public Wi-Fi and secure your devices. I may earn a commission on purchases made through this link.