BASH Heredoc Tutorial

BASH Heredoc Tutorial

How to use Here Document in bash programming

A block of code or text which can be redirected to the command script or interactive program is called here document or HereDoc. Different types of scripting language like bash, sh, csh, ksh etc. can take text input directly using here document without using any text file. So when the coder needs less amount of text data, then using code and data in the same file is a better option and it can be done easily by using here documents in a script. Without scripting language, here document can also be used in various high level languages like php, perl etc. How you can use here document in bash script is shown in this tutorial.

To use here document in any bash script, you have to use the symbol << followed by any delimiting identifier after any bash command and close the HereDoc by using the same delimiting identifier at end of the text. The syntax of writing HereDoc is shown below.

Command << HeredocDelimiter
. . .
. . .
HeredocDelimiter

Using Simple HereDoc Text

Suppose the command is `cat` and HereDoc delimiter is ADDTEXT. Open text editor and create a bash file named hd-example1.sh with the following code. When you use HereDoc in any script then it is necessary to keep the same name for both the starting and ending delimiter.

#!/bin/bash
cat <<ADDTEXT
This text is
added by Here Document
ADDTEXT

Now, go to the script folder and run the following command to execute the script. Here the script is stored in Documents folder.

$ bash hd-example1.sh

Using HereDoc with ‘-’ symbol

HereDoc uses ‘–‘ symbol to suppress any tab space from each line of heredoc text. In the following example, tab space is added at the start of each line and ‘–‘ symbol is used before the starting delimiter. When the script executes then all tab spaces are omitted from the starting of each line but it creates no effect on normal space. Here, a new file named hd-example2.sh is created to test the function of ‘–‘.

#!/bin/bash
cat <<-ADDTEXT2
Line-1: Here Document is helpful to print short text
Line-2: Here Document can be used to format text
Line-3: Here Document can print variable within the text
Line-4: Here Document with ‘-‘ removes tab space from the line
ADDTEXT2

Run the following command to execute the script and show the output.

$ bash hd-example2.sh

Using Variable within HereDoc Text

Create a new bash script with the following code. Here, two variables named start and end are declared. These variables are used within the hereDoc text. If you use quotation mark at the starting delimiter of HereDoc then the value of the variable will not print in the console.

#!/bin/bash
start="Hello everybody"
end="Good Luck"

cat <<ADDTEXT3
$start
Welcome to Linux Blog Site.
$end
ADDTEXT3

When you will execute the file then the following output will be shown.

Creating new bash file using HerDoc

In the previous examples, how you can use HereDoc in any bash script are shown.  You can also create new bash file using HereDoc which is shown in the next part of this tutorial. Create a new bash file named hd-example4.sh with the following code. Here, NewFile variable is declared to set the file name of new bash script which will be created after the execution of hd-example4.sh file.  After the execution, a new bash file named output.sh will be created. If you want, you can also run this file. The task of the new file is to multiply two numbers and print the result.

#!/bin/bash
NewFile=output.sh
(
cat <<‘ADDTEXT4’
#!/bin/bash

echo "This script creates a new file"

var1=10
var2=50

((result=$var1*$var2))
echo "The result = $result"
ADDTEXT4

) > $NewFile

Run the following commands to test the above script. The first command will execute the main script file. The second command will display the content of the newly created file. The third command will run the newly created bash file.

$ bash hd-exampl4.sh
$ cat output.sh
$ bash output.sh

Uisng Function with HereDoc

You can pass input values to the variables of any function of bash script from HereDoc content. Create another new bash file named hd-example5.sh to test how function can be used with HereDoc. Add the following code in the file. A function named BookInfo is declared in the script which will take data from HereDoc text. Six variables are declared in the function named ISBN, bookName, authorName, edition, publication and price. To set the value of the variables properly, you have to maintain the order of the values in HereDoc section according to the variables declared in the function. After setting the data in the function variables, price value is calculated by 10% discount and printed the values of all variables in the console.

#!/bin/bash
#Declare the function which will retrieve data from Here Document
BookInfo ()
{
read ISBN
read bookName
read authorName
read edition
read publication
read price
}
# Declare here document part to send data into the function
BookInfo <<ADDTEXT5
ISBN-78633567809
Easy Laravel 5
Jason Gilmore
9th Edition
Learpub
100
ADDTEXT5

# Print the value of the function variables after calculating price value with 10% discount
((price=$price$price*10/100))
echo "$bookName"
echo "$authorName"
echo "$edition, $publication"
echo "$"$price

Now, run the following command to execute the script and check the output.

$ hd-example5.sh

Here, the value of the price variable will set 100 from the HereDoc text. After setting 10% discount on price value, the value will be set as 90. So after execution, the following output will display in the console.

`cat` command is used in the most of the above examples. But you can also use any other bash commands with HereDoc. In the following example, another bash command wc is used to count the total lines and words of HereDoc text.

There are three lines and nine words in the above HereDoc text. Run the scripts to check the output.

$ bash hd-example6.sh

The examples which are shown in this tutorials will help you to understand the use of HereDoc in bash script. The use of here document in the bash script helps you to make the development task easier.

Related Posts
Leave a Reply

Your email address will not be published.Required fields are marked *