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
Basic Syntax in R Programming
R is the most popular language used for Statistical Computing and Data Analysis with the support of over 10, 000+ free packages in CRAN repository. Like any other programming language, R has a specific syntax which is important to understand if you want to make use of its powerful features. This art
3 min read
How to Run C program in Ubuntu
Learning to run a C program in Ubuntu is a great first step for anyone starting their programming journey or switching to a Linux-based system. Ubuntu offers a clean and powerful environment to write, compile, and execute C code all from the terminal. In C, this guide will show you how to get starte
4 min read
How to Compile and Run C program in Terminal?
To efficiently compile and run C programs, users typically utilize a compiler or the built-in terminal. The command prompt (CMD) on Windows can be employed to process C programs and generate the desired outputs. To create an executable C program, users must compile their C code using the system term
6 min read
How to Use Heredoc in Shell Scripting
The Heredoc or Here Document is an input literal used by many programming and scripting languages. The Heredoc was originally used in UNIX Shells and is in fashion till date and, is supported by all popular Linux shells such as zsh, bash, tsh, etc. The symbol for a Heredoc is '<<' two left che
6 min read
Bash Script - How to use Command Line Arguments
In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
Hello World in R Programming
When we start to learn any programming languages we do follow a tradition to begin HelloWorld as our first basic program. Here we are going to learn that tradition. An interesting thing about R programming is that we can get our things done with very little code. Before we start to learn to code, le
2 min read
Bash Scripting - How to check If File Exists
In this article, we will write a bash script to check if files exist or not. Syntax :test [expression][ expression ][[ expression ]] Here, in expression, we write parameter and file name. Let us see some parameters that can be used in the expression: - -f: It returns True if the file exists as a com
6 min read
How to Customize Bash Colors and Content in Linux Terminal Prompt
If you are using the Linux operating system, that means you use the CLI most of the time. And do more work on the terminal. By default, most Linux Operating systems provide you the bash shell. Shell provides the interface between the user and kernel and executes commands. In this article, we are goi
9 min read
How to Use file.path() Function in R
R programming language is becoming popular among developers, analysts, and mainly for data scientists. Students are eagerly learning R with Python language to use their analytical skills at their best. While learning any language, one is faced with many difficulties, and the individual learning R Pr
3 min read
How to Exit When Errors Occur in Bash Scripts
When writing Bash scripts, it's essential to include error handling to ensure that the script exits gracefully in the event of an error. In this article, we'll cover all the possible techniques for exiting when errors occur in Bash scripts, including the use of exit codes, the "set -e" option, the "
7 min read