What is HereDoc Variable in Bash? [5 Practical Cases]

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
EXPLANATION
  • 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.

Opening the file in Nano text editor➌ 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"
EXPLANATION

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
EXPLANATION
  • 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.

Adding executable permission to the script➏ Finally, run the script by the following command:

./multi.sh

Created multiline message using Bash HereDoc variableFrom 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.

You can follow the Steps of case 1, to save & make the script executable.

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"
EXPLANATION

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

Redirection Using HereDoc VariableFrom 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:

You can follow the Steps of case 1, to save & make the script executable.

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"
EXPLANATION

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

HereDoc variable as input to a _grep_ commandThe 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:

You can follow the Steps of case 1, to save & make the script executable.

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."
EXPLANATION

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

A dynamic html page generation using HereDoc variableThe 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:

You can follow the Steps of case 1, to save & make the script 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."
EXPLANATION

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

Created a custom script and made it executableThe 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

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.

Related Articles


<< Go Back to Types of Variables in Bash | Bash Variables | Bash Scripting Tutorial

Rate this post
Nadiba Rahman

Hello, This is Nadiba Rahman, currently working as a Linux Content Developer Executive at SOFTEKO. I have completed my graduation with a bachelor’s degree in Electronics & Telecommunication Engineering from Rajshahi University of Engineering & Technology (RUET).I am quite passionate about crafting. I really adore exploring and learning new things which always helps me to think transparently. And this curiosity led me to pursue knowledge about Linux. My goal is to portray Linux-based practical problems and share them with you. Read Full Bio

Leave a Comment