How to use Here Document in bash programming
Last Updated :
16 Mar, 2023
A shell script is a sequence of commands that are written in a plain text file and executed by the shell program in Unix-like operating systems. The shell is a command-line interface that provides users with a powerful way of automating various tasks, including file management, process control, and system administration. One of the most useful features of shell scripting is the ability to use "here documents," which allow you to embed input data directly into your shell scripts.
Here documents are a way of sending multiple lines of text to a command, such as a shell script, as standard input. They are often used to pass multi-line strings to commands, including shell scripts, which can then process them as a single string. This is particularly useful when you need to pass a large block of data as input to a shell script, such as a list of users or a set of commands to be executed.
Here is a basic example of a shell script that uses a here document:
#!/bin/bash
# This is a simple script that uses a here document
# to pass a list of users to the `sort` command
sort << EOF
John Doe
Jane Doe
Jim Smith
Jane Smith
EOF
Let's break down the script to understand what's happening:
- The first line of the script, #!/bin/bash, is known as the shebang line and is used to specify the shell that should be used to interpret the script. In this case, we're using the bash shell.
- The next line is a comment that provides a brief description of what the script does.
- The sort command is used to sort the list of users. This is the main part of the script, and it's where the here document comes into play.
- The << operator is used to introduce the here document. The text that follows the operator is known as the "marker" and is used to specify the end of the here document. In this case, the marker is EOF.
- The list of users is then written between the start and end markers. This list of users is then passed as standard input to the sort command, which sorts the list in alphabetical order.
- Finally, the end marker is specified on a line by itself to indicate the end of the here document.
When you run this script, the output will be:
Jane Doe
Jane Smith
Jim Smith
John Doe
As you can see, the sort command has sorted the list of users in alphabetical order.
1. Structure of Here Documents:
The structure of a here document is as follows:
command << [marker]
data
[marker]
The command is the shell command that will receive the input data. The << operator is used to introduce the here document, and the text that follows the operator is the "marker" which specifies the end of the here document. The data is the input data that will be passed to the command as standard input, and the marker is specified on a line by itself to indicate the end of the here document.
Here is an example of sending a message to everyone logged in using a here document:
#!/bin/bash
# This is a simple script that uses a here document
# to send a message to everyone logged in
wall << EOF
Attention all users:
The system will be undergoing maintenance in the next hour.
Please save your work and log out.
EOF
When you run this script, the message "Attention all users: The system will be undergoing maintenance in the next hour. Please save your work and log out." will be sent to everyone logged in to the system.
2. Multi-line message using cat:
Here is an example of using a here document with the cat to create a multi-line message:
#!/bin/bash
# This is a simple script that uses a here document
# with the `cat` command to create a multi-line message
message=$(cat << EOF
Hello there!
This is a multi-line message created using a here document.
EOF
)
echo "$message"
When you run this script, the output will be:
Hello there!
This is a multi-line message created using a here document.
3. Use of Here Documents with '-' Symbol:
The - symbol can be used instead of a marker to indicate the end of a here document. When the - symbol is used, it allows you to preserve leading tabs in your input data. This is useful when you want to preserve the formatting of your input data, such as in the following example:
#!/bin/bash
# This is a simple script that uses a here document
# with the `-` symbol to preserve leading tabs
message=$(cat <<- EOF
Hello there!
This is a message with preserved leading tabs.
EOF
)
echo "$message"
When you run this script, the output will be:
Hello there!
This is a message with preserved leading tabs.
4. Using Here Documents with Pipes to Search and Replace Content:
Here documents can also be used with pipes to search and replace content. In the following example, we use the sed command to search and replace the word "world" with "shell scripting":
#!/bin/bash
# This is a simple script that uses a here document
# with pipes to search and replace content
message=$(cat << EOF | sed 's/world/shell scripting/g'
Hello world!
EOF
)
echo "$message"
When you run this script, the output will be:
Hello shell scripting!
5. Handling Tab Characters:
It is important to handle tab characters properly when using here documents, as they can cause unexpected results. For example, if a tab character appears in your input data, it will be interpreted as a tab by the shell, not as a series of spaces. To handle tab characters properly, you can either use the - symbol to preserve leading tabs, or you can replace tabs with spaces in your input data.
6. Sends the message to everyone logged in:
Here documents can also be used to send messages to everyone currently logged in to a system. This is done by using the write command, as shown in the following example:
#!/bin/bash
# This is a simple script that uses a here document
# to send a message to everyone currently logged in
# Find all users currently logged in
users=$(who | awk '{print $1}')
# Loop through each user and send them a message
for user in $users; do
# Use a here document to send the message
message=$(cat << EOF
Hello $user,
This is a message sent to everyone currently logged in.
EOF
)
# Use the `write` command to send the message to the user
echo "$message" | write $user
done
When you run this script, it will find all users currently logged in and send each of them a message using the write command. The message will be sent to their terminal and will be displayed as if it were typed into the terminal directly. Note that this script will only work if the write command is available on your system.
Conclusion
In conclusion, here documents provide a convenient way to pass multi-line strings to shell scripts, making it easier to automate complex tasks. They are a useful tool to have in your shell scripting toolkit and are often used to pass data as input to scripts that perform data processing or system administration tasks
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Linux Commands Cheat Sheet
Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read