Bash Heredoc | Explained

The HereDocument (heredoc) is a type of redirection that allows the users to pass several lines of data as input via the command or script. The heredoc method is commonly used in Bash scripts to automate the input and run the commands line-by-line to run the programs.

This guide will explain different uses of the heredoc method with the following supporting content:

How to Use the Bash Heredoc Method?

The heredoc method can take several lines of data as input via the bash script. This section will explain the methods to use the heredoc in Linux.

Let’s first understand the basic syntax of the heredoc method.

The general syntax for the bash heredoc method is given below:

Command <<[-] Delimiter
<Heredoc Content>
DELIMITER

The general syntax explanation is as follows:

  • Command: It can be replaced with any command that accepts the redirection.
  • <<: It redirects the output of the heredoc method to the command.
  • : The dash symbol () can also be used optionally to suppress/remove the tab space.
  • Delimiter: The delimiter contains the content of the heredoc method, which is defined between the delimiter keyword and ends with the same delimiter keyword. Common delimiters are EOF, EOT, and END, while any word can be used as a delimiter.
  • Heredoc Content: The heredoc content contains several lines of input data or commands.

Example 1: Use Multiple Lines with Heredoc Method

We can use the heredoc method to redirect its output to the cat command, which displays the output of the heredoc method. For example, the below bash script takes three input lines of data from the heredoc method, which will be redirected to the cat command to display the output on the terminal:

#!/bin/bash
cat <<- EOT
Welcome
To
Itslinuxfoss
EOT

The above heredoc syntax shows the following things:

  • #!/bin/bash: It starts the bash script.
  • cat <<: The output is redirected to the cat command, which displays these data lines.
  • EOT: It is the delimiter used in this example.

The above bash script is saved to a bash file, “heredoc.sh”, used in this article.

To execute the bash script, use the below command:

$ bash heredoc.sh

The output displays multiline data as the output of the cat command, which is defined in the heredoc method.

We can use the bash script with the dash symbol before the heredoc delimiter to remove the space before the text. For instance, the below code will have space before the text that will be removed from the output using the symbol:

#!/bin/bash
cat <<- EOT
        Welcome
        To
        Itslinuxfoss
EOT

To execute the bash script, use the below command:

$ bash heredoc.sh

The output shows that the space is removed before the text in the heredoc method using the dash symbol () before the delimiter.

Example 2: Write Multiple Lines to a File with Heredoc Method

Several lines of data or commands are specified within the heredoc notation whose output can be saved to a file. For instance, the below bash script saves the “ls” and “hostname” commands to a text file named “testfile.txt”:

#!/bin/bash
cat << EOT >> testfile.txt
Welcome to Bash Script.
EOT

Note: The double redirect operator (>>) after the EOT delimiter is used to append the data into a file. If you want to overwrite the content of the previous file, use the single redirect operator (>) here.

Execute the bash script and check the content of testfile.txt by using the below commands:

$ cat testfile.txt
$ bash heredoc.sh

The output of the two commands is written to testfile.txt, as shown in the output.

Example 3: Use Variables With the Heredoc Method

The variables can be defined within the bash script and used within the heredoc method. For instance, the below bash scripts declare two variables, num1, and num2, that will be used to sum within the heredoc method:

#!/bin/bash
num1=2
num2=3
cat << EOT
The sum of $num1 and $num2 is: $(($num1+$num2))
EOT

To execute the bash script, use the below command:

$ bash heredoc.sh

The output shows the result of two variables sum within the heredoc method.

Example 4: Use Functions With the Heredoc Method

We can use the functions with the heredoc method to take the arguments from the heredoc. For instance, the below code will take two arguments, “name” and “age” defined in the function from the heredoc method. The output of the arguments taken by the function can be displayed using the echo command as shown below:

#!/bin/bash

readLines(){
        read name
        read age
}

readLines << EOF
Welcome $USER!
23
EOF

echo $name
echo $age

To execute the bash script, use the below command:

$ bash heredoc.sh

The output shows that the arguments passed to the function from the heredoc method “$USER” and “23” are shown on the output.

Example 5: Execute Command Within the Heredoc Method

We can execute the command specified within the heredoc. For instance, the below bash script executes two commands, “pwd” and “date”, specified within the heredoc, using the “sudo” user with the “s” option to display on the terminal:

#!/bin/bash
cat << EOT
$(pwd)
$(date)
EOT

To execute the bash script, use the below command:

$ bash heredoc.sh

The two specified commands within the heredoc output are displayed above.

Example 6: Escape the Special Characters With the Heredoc Method

We can use the three methods to escape the special characters within the bash heredoc method that are:

  • Escape characters using single quotes (‘’)
  • Escape characters using double quotes (“”)
  • Escape characters using the backslash (\)

For instance, to escape the two special characters Environment variable “SHELL” and command “whoami” can be escaped and taken as normal text using the below three syntaxes:

Escape characters using single quotes (‘’)

The single quotes (‘’) can be used with the first delimiter to escape the special characters:

#!/bin/bash
cat << 'EOT'
The current shell is ${SHELL} shell.
$(whoami)
EOT

Escape characters using double quotes (‘’)

We can use double quotes (“”) instead of single quotes (‘’) to escape the special characters such as commands and environment variables:

#!/bin/bash
cat << "EOT"
The current shell is ${SHELL} shell.
$(whoami)
EOT

Escape characters using the backslash (\)

Alternatively, the backslash (\) can be used before the first delimiter to escape all the special characters with the heredoc:

#!/bin/bash
cat << \EOT
The current shell is ${SHELL} shell.
$(whoami)
EOT

To check the above three bash script syntaxes, the output can be checked using the below bash command:

$ bash heredoc.sh

The output shows that special characters and variables are escaped that are displayed as the normal text instead of commands. The above three methods show the same output.

Example 7: Count Lines Using Bash Heredoc Method

We can use the wc command that counts the numbers of words and lines in a document with the bash heredoc method. The below bash script counts the numbers of lines specified within the heredoc method:

#!/bin/bash
wc -l << EOF
Hello
itslinuxfoss
Viewers.
EOF

To execute the bash script, use the below command:

$ bash heredoc.sh

The output shows 3 lines in the heredoc method.

Example 8: Find a Specific Word Using Heredoc Method

We can use the grep command in the heredoc method to find specific words from the lines. For instance, the below bash script code finds the word “itslinuxfoss” from the text:

#!/bin/bash
$ cat << EOT | grep -i itslinuxfoss
Hello! Welcome to itslinuxfoss.
Warm wishes to itslinuxfoss viewers.
EOT

To execute the bash script, use the below command:

$ bash heredoc.sh

Two lines are displayed on the output that contains the word “itslinuxfoss”.

Example 9: Use Heredoc Method Within the Loop

We can use the heredoc method in different loops. For example, the below bash script uses the heredoc method within the if statement that will redirect the heredoc method output to the cat command:

#!/bin/bash

if true;
then
        cat <<- EOT
Welcome
Itslinuxfos
EOT
fi

To execute the bash script, use the below command:

$ bash heredoc.sh

It shows the output of the heredoc method defined within the if statement.

Conclusion

The bash heredoc method can be used to display multiple lines of data or commands using the bash script to perform a specific task. This guide explains several uses of the bash-heredoc method with the help of examples.