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.
Key Takeaways
- Learning about Bash HereDoc variables.
- Exploring the readability and flexibility of the HereDoc variables in Bash.
Free Downloads
What is a HereDoc in Bash?
HereDoc stands for Here Document, 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.
Basic Syntax >
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 some cases of Bash HereDoc variable:
Case 1: Creating Multiline Message Using HereDoc Variable in Bash
To create multiline messages using HereDoc variables in Bash follow the step-by-step procedures below:
Steps to Follow >
➊ Open your Ubuntu Terminal.
➋ To open a script in the nano text editor, write the command below:
nano multi.sh
- nano: A text editor.
- multi.sh: This is a script. Here, I have named the script ‘multi.sh’. You can name any of your choices.
➌ Hereafter, write the following script inside the editor:
Script (multi.sh) >
#!/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 #!/bin/bash, ‘#!’ is called ‘Shebang’ or ‘Hashbang’. Now, ‘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.
➍ Then, press CTRL+S to save the file & press CTRL+X to exit.
➎ After that, use the command below to make the script executable:
chmod u+x multi.sh
- chmod: Changes the permission of the files and directories.
- u+x: Adds the executable permission for the user.
- multi.sh: The file which you want to make executable.
➏ Finally, run the script by the following command:
./multi.sh
From the image, you can see the multiline message which I have created using the HereDoc variable.
Case 2: Content Redirection 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.
Script (redirect.sh) >
#!/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.
Now, run the script by the following command:
./redirect.sh
From the image, you can see that the content of the HereDoc variable has been redirected to the file ‘file.txt’.
Case 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:
Script (input.sh) >
#!/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’.
Now, run the script by the following command:
./input.sh
The above image shows the matched pattern in the HereDoc variable.
Case 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:
Script (html.sh) >
#!/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 variables ‘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.
Now, run the script by the following command:
./html.sh
The above image shows the creation of a dynamic HTML page ‘file.html’ using the HereDoc variable.
Case 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:
Script (custom.sh) >
#!/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.
Now, run the script by the following command:
./custom.sh
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
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