FUNDAMENTALS A Complete Guide for Beginners
Being a Linux user, while working with complex or lengthy content, it’s so usual to face a situation like when you need to redirect a block of text or code stream to an interactive command. In such a case, the HereDoc variable serves as the best option by embedding the multiline strings directly within the Bash scripts. However, let’s explore this article to have a concise look at different aspects of HereDoc variables in Bash.
What is a “HereDoc” in Bash?
HereDoc (Here Document) is a feature that allows you to define multiple lines of text within your code in Bash. Generally, it starts with a delimiter and ends with the same delimiter at the beginning of a line. And inside the delimiter, you can include any kind of plain text or variable reference as HereDoc content.
Here’s a basic syntax of HereDoc in Bash:
COMMAND << DELIMITER
HereDoc Text
DELIMITER
What is Bash “HereDoc” Variable?
A Bash HereDoc variable is a powerful tool that uses the HereDoc construct, captures multiple content blocks, and assigns it to a variable. This variable is useful not only for storing configuration files, complex strings, and variable references but also for containing command substitutions in case of making dynamic content.
5 Cases for Bash “HereDoc” Variable
There are various ways to use the HereDoc variable in Bash. The following section showcases 5 cases of Bash HereDoc variable:
1. Create Multiline Message Using “HereDoc” Variable in Bash
Multiline messages can be created with the HereDoc variable in bash. Here document allows you to define a block of messages within a script or command. It can include multiline text without escaping any special characters.
Now, to create multiline messages using HereDoc variables in Bash, follow the script:
#!/bin/bash
name="X"
multi_msg=$(cat <<EOF
Hello, $name!
This is an example
It is a multiline message with HereDoc variable
EOF
)
echo "$multi_msg"
In this script, name="X"
assigns the value X to the variable name. Then, the line multi_msg=$(cat <<EOF … EOF)
specifies a HereDoc section. The text between the delimiter <<EOF
and EOF
is assigned to the variable multi_msg. Next, in the line echo "$multi_msg"
, the echo command prints the text of the multi_msg variable.
From the image, you can see the multiline message that I have created using the HereDoc variable.
2. Redirect Content Using “HereDoc” Variable in Bash
You can use the HereDoc variable to define text and then redirect the content to a file. This lets you overwrite a file with the stored text of the HereDoc variable.
Let’s see a bash script of how to redirect content using the HereDoc variable in bash:
#!/bin/bash
output_file="file.txt"
text=$(cat <<EOF
Hello, there!
This symbolizes HereDoc variable redirection.
EOF
)
echo "$text" > "$output_file"
echo "Text has been redirected to $output_file"
Here, the line output_file="file.txt"
assigns a file file.txt to the variable output_file. Next, text=$(cat <<EOF … EOF)
specifies a HereDoc block. The text between the delimiter <<EOF
and EOF
is assigned to the variable text. Then, the line echo "$text" > "$output_file"
redirects the content of the text variable to the file assigned to the output_file variable. Finally, in echo "Text has been redirected to $output_file"
, the echo command prints Text has been redirected to $output_file to confirm the redirection of the HereDoc content.
From the image, you can see that the content of the HereDoc variable has been redirected to the file file.txt.
3. Bash “HereDoc” Variable as Input for “grep” Command
The following example demonstrates how you can use the HereDoc variable as input to the grep command to search for specific patterns. Check the bash script to know how to use the HereDoc variable as input of the grep
command:
#!/bin/bash
#Defining a grep pattern
grep_pattern="linuxsimply"
#Defining 'content' as a HereDoc variable
content=$(cat <<EOF
This line contains 'linuxsimply'.
Search for: $grep_pattern
EOF
)
#Using 'grep' for searching the matching lines with the defined pattern
echo "$content" | grep "$grep_pattern"
Here, grep_pattern="linuxsimply"
assigns the pattern linuxsimply to the HereDoc variable grep_pattern. Next, content=$(cat <<EOF … EOF)
specifies a HereDoc block. The text with the specified search pattern between the delimiter <<EOF
and EOF
is assigned to the variable content. Now, in the line echo "$content" | grep "$grep_pattern"
, the text inside the content variable is piped to the grep
command where the command searches for lines that match with the specified pattern in the variable grep_pattern.
The above image shows the matched pattern in the HereDoc variable.
4. Bash “HereDoc” Variable for Dynamic HTML Generation
The following example is a basic illustration of the HereDoc variable in case of generating a simple & dynamic HTML page along with an HTML file including dynamic data. Let’s check the bash script to learn how to use the bash HereDoc variable for Dynamic HTML generation:
#!/bin/bash
title="A Dynamic Website"
content="<h1>Welcome to LinuxSimply</h1>"
html_file=$(cat <<EOF
<!DOCTYPE html>
<html>
<head>
<title>$title</title>
</head>
<body>
$content
</body>
</html>
EOF
)
#Redirecting the html content to a file
echo "$html_file" > file.html
echo "A dynamic html page 'file.html' is created."
Here, title="A Dynamic Website"
assigns the string A Dynamic Website to the variable title and content="<h1>Welcome to LinuxSimply</h1>"
assigns the string <h1>Welcome to LinuxSimply</h1> to the variable content. Then, a HereDoc variable is defined in the line html_file=$(cat <<EOF
and the content including the variable’s title and content between the delimiter <<EOF
and EOF
are assigned to the variable html_file.
Afterward, in the line echo "$html_file" > file.html,
the content of the html_file variable is redirected to the file file.html. Finally, the echo
command displays A dynamic html page ‘file.html’ is created. to confirm the creation of the dynamic HTML page.
The above image shows the creation of a dynamic HTML page file.html using the HereDoc variable.
5. Bash “HereDoc” Variable for Creating a Custom Script
Here’s an example of how you can use the HereDoc variable for defining the content of your own customized script, redirect it to a file, and make it executable. Check the script for creating a custom script using the HereDoc variable:
#!/bin/bash
#Defining a custom script
custom=$(cat <<'EOF'
echo "This is a customized script."
echo "It performs an effective task."
EOF
)
#Redirecting the content of the 'custom' variable to a file
echo "$custom" > script.sh
#Making the script executable
chmod u+x script.sh
echo "A custom script 'script.sh' is created and made executable."
In the line custom=$(cat <<'EOF'
, a HereDoc variable is defined, and the content between the delimiter <<'EOF'
and EOF
are assigned to the variable custom. Here, <<'EOF'
and EOF
are used to prevent the variable expansion. Next, the content of the custom variable is redirected to a custom script script.sh.
After that, in the line chmod u+x script.sh,
the chmod command makes the script executable allowing permission to the owner of the file. Finally, the echo
command displays A custom script ‘script.sh’ is created and made executable. indicating that the custom script is successfully created and made executable.
The above snapshot depicts that I have created a custom script named script.sh and made it executable.
Conclusion
So far, from the whole article you have got that the HereDoc variable is such a flexible tool in Bash that enables you to create dynamic files, manipulate multiline contents, etc. By leveraging this variable properly, you can handle complex text-based tasks in your scripts in a powerful and effective way.
People Also Ask
What is the syntax of the HereDoc?
The syntax of HereDoc (here document) is:
COMMAND << DELIMITER
HereDoc Text
DELIMITER
How to redirect HereDoc content to a file?
To redirect HereDoc content to a file, you can use the following syntax:
cat <<EOF > file.txt
hereDoc content.
EOF
The content of HereDoc will be redirected to the file.txt file.
How to prevent variable expansion within a HereDoc?
You can prevent variable expansion within a HereDoc by enclosing the delimiter in single quotes (‘ ’).
What to do if I want to maintain indentation within the HereDoc?
If you want to maintain indentation within the HereDoc, then you need to ensure the indentation of the heredoc delimiter along with the content.
Can I use a HereDoc variable to provide input to a command?
Yes, you can use a HereDoc variable when you want to append multiple lines of input to a command.
How can I remove line breaks from the HereDoc content?
Append the tr command to remove line breaks from the HereDoc content before assigning it to a variable.
Can I use command substitution within a HereDoc?
Yes, you can use command substitution within a HereDoc.
What is cat <<EOF in bash?
The cat <<EOF
construct is known as a here document in bash which is used to contain a block of text within a script or command. The EOF delimiter stands for the end of file that indicates the beginning and end of a here document.
Related Articles
- What Are Built-in Variables in Bash [2 Cases With Examples]
- An Ultimate Guide of Using Bash Environment Variables
- The “.bashrc” Environment Variables [4 Cases]
- String Variables in Bash [Manipulation, Interpolation & Testing]
- What is Variable Array in Bash? [4 Cases]
- An Extensive Exploration of Bash Special Variables [9 Examples]
- What is Boolean Variable in Bash? [3 Cases With Examples]
- What is PS1 Variable in Bash? [3 Customization Examples]
- What is PS3 Variable in Bash? [3 Practical Examples]
<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial