Open In App

How to use Here Document in bash programming

Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. 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.
  2. The next line is a comment that provides a brief description of what the script does.
  3. 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.
  4. 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.
  5. 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.
  6. 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


Last Updated : 16 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads