Here Document in Bash

Fumbani Banda Jan 20, 2022
  1. What Is Here Document in Bash
  2. Use Here Document to Put a Multi-Line String to a File in Bash
  3. Use Here Document to Write Scripts Interactively in Bash
  4. Use Here Document to Pass Data to a Function in Bash
  5. Turn Off Parameter Substitution in Here Document in Bash
Here Document in Bash

This tutorial explains what the here document is and its usage to write a multi-line string to a file, write Bash scripts interactively, use it with functions, and turn off parameter substitution.

What Is Here Document in Bash

Heredoc is short for here document. Heredoc uses an interactive program to provide input to a command. The heredoc uses Here Tag to indicate the end of input to a command.

The first Here Tag defines the Here Tag that will indicate the end of the input. The notation used for here document is shown below.

command <<Here Tag
>command1
>command2
>command3
>Here Tag

The first Here Tag is preceded by the << symbol. This symbol directs the interactive input for the here document to command as standard input for further processing.

When defining a Here Tag, be sure that it should not appear in the command list to avoid confusing matters.

Use Here Document to Put a Multi-Line String to a File in Bash

We use the here document and the cat command to input a multi-line string to a text file interactively. The EOF that comes after the << symbol defines the Here Tag for the here document.

It means that we will input a multi-line string on the interactive shell until the shell sees the Here Tag, EOF, in our case. The output redirection operator, >, is used to direct the standard output of the cat command to the specified file, output.txt, in this case.

cat <<EOF > output.txt
> Hello user.
> Welcome to DelftStack.
> Hope you enjoy your stay.
> EOF

Using the symbol, >>, as the redirection operator appends the output of the cat command to the specified file while the > symbol creates a new file, and if the file already existed, it re-writes the contents of the file.

Use Here Document to Write Scripts Interactively in Bash

We use the here document and the cat command to write a Bash script interactively and redirect the output to a Bash script. We define the Here Tag after the << symbol and delimit the end of the interactive input with the Here Tag.

The Here Tag in the script below is EOF. We use the output redirection operator, >, to redirect the output to the Bash script, echo.sh.

cat <<EOF > echo.sh
> #!/bin/bash
>
> echo "Hello World!"
> echo "Welcome to DelftStack."
> EOF

Using the cat command, we check the contents of the echo.sh script.

cat echo.sh

We successfully wrote the Bash script using the here document from the output below.

#!/bin/bash

echo "Hello World!"
echo "Welcome to DelftStack."

Use Here Document to Pass Data to a Function in Bash

The script below reads the input from the here document. We have defined a function in the script called GetPersonalInfo. The function has commands that read input data.

We used the here document to pass the input data to the function and used the echo commands to display the data to the standard output. The variables defined inside the function are accessed outside of the function because variables in Bash are global by default unless the local keyword has defined them.

#!/bin/bash
GetPersonalInfo(){
    read fname
    read lname
    read country
}

GetPersonalInfo <<EOF
John
Doe
USA
EOF

echo "Personal Information"
echo
echo "First Name: $fname"
echo "Last name: $lname"
echo "Country of Residence: $country"

Executing the Bash script produces the following output.

Personal Information

First Name: John
Last name: Doe
Country of Residence: USA

Turn Off Parameter Substitution in Here Document in Bash

We generate a new script from the existing script using the here document in the script below. The Here Tag, EOF, has been quoted using single quotes to avoid parameter substitution inside the here document.

Quoting the first Here Tag tells the here document to preserve the literal meaning of special characters inside the here document.

#!/bin/bash
echo "This script generates another script."

cat <<'EOF' > add.sh
#!/bin/bash

x=10
y=11

let "sum = $x + $y"
echo "Total of $x and $y is $sum"
EOF

We have used the cat command to check the contents of the newly generated script, add.sh.

#!/bin/bash
x=10
y=11

let "sum = $x + $y"
echo "Total of $x and $y is $sum"

Executing the newly generated script produces the following output to the terminal.

Total of 10 and 11 is 21
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub