Here Document And Here String in Bash on Linux


Introduction

Bash is a Unix shell and command language that is commonly used in Linux and other Unix-based operating systems. It is a powerful tool for automating tasks, managing system configurations and performing various other operations on a Unix-based system. Bash provides several features that make it a powerful and flexible tool for users, including support for "here documents" and "here strings."

What is a “Here Document”?

A “here document”, also known as a "here doc", is a special type of input redirection that allows a user to specify multiple lines of input for a command. This is particularly useful for specifying long blocks of text or script code as input to a command.

Here documents are specified using a << operator followed by a delimiter, which can be any string. The lines of input are then specified after the delimiter and are terminated by a line containing only the delimiter, followed by a semicolon (;). For example, the following command uses a here document to specify multiple lines of input for the cat command −

$ cat <<EOF
This is line 1 of the input.
This is line 2 of the input.
This is line 3 of the input.
EOF

In this example, the delimiter is "EOF" (which stands for "End of File"). The lines of input are specified between the delimiter and the terminating line containing only the delimiter. When the command is executed, the cat command reads the input and displays it on the screen.

“Here documents” can also be used to specify input for commands that take script code as input, such as awk or perl. For example, the following command uses a here document to specify a script for the awk command 

$ awk '{print $1}' <<EOF
This is line 1 of the input.
This is line 2 of the input.
This is line 3 of the input.
EOF

In this example, the awk command will read the input and execute the script specified in the here document, which simply prints the first field (such as column) of each line of input.

What is a Here String?

A “here string” is similar to a here document, but it is specified using a single-quote (') instead of a double less-than (<<) symbols. Like a “here document”, a here string allows a user to specify multiple lines of input for a command. However, a here string is interpreted as a single string, rather than multiple lines of input.

“Here strings” are specified using a single-quote followed by a delimiter, which can be any string. The string is then specified after the delimiter and is terminated by a line containing only the delimiter, followed by a single-quote. For example, the following command uses a here string to specify a multi-line string as input for the echo command 

$ echo 'This is a string
that spans multiple
lines.'

In this example, the delimiter is the single-quote itself. The string is specified between the delimiter and the terminating line containing only the delimiter. When the command is executed, the echo command reads the input and displays it on the screen as a single string.

Here strings can also be used to specify input for commands that take script code as input, such as awk or perl. For example, the following command uses a here string to specify a script for the awk command 

$ awk '{print $1}' 'BEGIN {FS=":"}
{print $1}'

In this example, the awk command will read the input and execute the script specified in the here string, which sets the field separator to a colon (:) and then prints the first field of each line of input.

Using “Here Documents” and “Here Strings”

“Here documents” and “here strings” can be useful in a variety of situations when working with Bash. Some common use cases include 

  • Specifying long blocks of text or script code as input to a command.

  • Creating scripts or commands that are easier to read and maintain by breaking them up into multiple lines.

  • Simplifying the process of specifying input for commands that require multiple lines of input.

Here is an example of a script that uses a “here document” to create a new file and write some text to it

#!/bin/bash
# Create a new file called "output.txt"
cat > output.txt <<EOF
This is line 1 of the output.
This is line 2 of the output.
This is line 3 of the output.
EOF

This script uses the cat command with the > operator to redirect output to a new file called "output.txt". The here document is then used to specify the text that should be written to the file. When the script is executed, the cat command writes the text specified in the here document to the "output.txt" file.

Here is an example of a script that uses a here string to pass a multi-line string as an argument to the grep command 

#!/bin/bash
# Search for a pattern in a multi-line string
grep 'pattern' 'line 1
line 2
line 3'

In this example, the grep command searches for the string "pattern" in the multi-line string specified in the here string. If the string is found, the grep command will print the matching line or lines to the screen.

Conclusion

“Here documents” and “here strings” are useful features in Bash that allow a user to specify multiple lines of input for a command. They can be used to specify long blocks of text or script code as input or to simplify the process of specifying input for commands that require multiple lines of input. By using here documents and here strings, users can create scripts and commands that are easier to read and maintain, and can automate various tasks on a Unix-based system more effectively.

Updated on: 04-Jan-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements