Conclusions

This article discussed how to use the Heredoc in Shell Scripting. We discussed some of the commonly used delimiters with the Heredoc. Then, we explored all other possibilities of using a Heredoc in a shell script with extensive examples. 


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 chevrons.

Syntax

<< 'Delimiter'
---statements/comments---
'Delimiter'

A Heredoc is followed by a delimiter token, which in turn is followed by a content block. The block ended with the same delimiter token. 
There are various delimiters that can be used such as BLOCK COMMENT, etc. In this article, we shall discuss some of the major and commonly used delimiter tokens.

Similar Reads

Using Heredoc with SSH

Now, being a handy tool, the Heredoc can be used with SSH to execute multiple commands at the remote server. We shall see the same with an example....

Executing commands with Heredoc

The block delimiter is followed by the << which, is followed by a shell command. This allows the user to execute multiple commands from a single block, or multiple statements with a single command. See the following example where we use the CAT shell command to print multiple input lines....

Using the ‘-‘ with Heredoc – White/Tab space suppression

The Heredoc (<<) does not suppress additional tabs and whitespaces on default settings. We add a hyphen (-) after the Heredoc to suppress tabs but, no whitespaces. See the following example....

The COMMENT delimiter

In Shell Scripting, comments are followed by the # literal. However, for multiline comments, adding the # as a suffix to each line is not an efficient way to create multiline comments. To do the same in a more programmatic way, we can use the Heredoc with COMMENT delimiter. See the following example to understand how....

Use of variable in Heredoc text

We can perform variable substitution with Heredoc in bash. The variable usage is similar to a normal bash script, by using the ${} notation. See the following example....

Escaping special characters

We can use the backslash (\) literal to escape special characters either in an entire block or a specific line in a block.While escaping an entire block, we can do one of the following:...

Create a new bash file with Heredoc

To create a new bash file with Heredoc, one should use the following syntax:...

Heredoc with functions

We can use Heredoc with functions to pass them multiple arguments to the function call. In the following example, we will create a function to take a person’s details and then, display the same....

Piping/Redirecting and Command substitution

In this section, we shall understand how to use commands inside a Heredoc and how to pipe/redirect the output of a Heredoc to another command....

Conclusions

This article discussed how to use the Heredoc in Shell Scripting. We discussed some of the commonly used delimiters with the Heredoc. Then, we explored all other possibilities of using a Heredoc in a shell script with extensive examples....