Introducing and use Heredoc on Ubuntu, Centos, Debian

Introducing and use Heredoc on Ubuntu, Centos, Debian

Heredoc or here document is generally used in computer science for the specification of command-line interpreters. Heredoc is also designed to be used to define sections of text. Some command-line interpreters such as bach, Windows PowerShell, and the Bourne Shell are specified by Hereduc and programming languages like Perl, PHP, Python, and ruby. As a file literal or input stream literal, Hereduc is a section of a source code file. Join us with this article by Introducing and use Heredoc on Ubuntu, Centos, Debian. To provide your own Linux VPS and use 2021 offers visit Eldernode and purchase a package.

Introducing Heredoc on Linux Ubuntu, Centos, Debian

Recommended Article: Linux Server Monitoring Commands

Introduction To Heredoc

Heredoc is used for specifying input values for command-line commands for Unix shells like Bourne shell and zsh. For example, in Bash and other shells like Zsh, Heredoc is a type of redirection that allows you to pass multiple lines of input to a command. It would be different due to what programming language you are using, however, will be <<, <<<, <! –,<< -, or like string initiated, followed by arbitrary delimiter specifiers. You are free to use any string as a delimiting identifier, but EOF (End Of File) or END are used most. The end of the Heredoc is marked by a line with the previously selected identifier or a terminating semicolon if needed. You can use Heredoc in your shell scripts since Heredoc is mostly used in combination with the cat command. Also, to execute multiple commands on a remote system over SSH, Heredoc is a very easy way.

How to install Heredoc

You need a terminal to be able to initiate a heredoc. It is possible to use the syntactic rick in Bash, tsch, and Korn shell.

Look at the command below:

$ cat << EOF >> example.txt

While you are typing into your terminal during the loop, is piped into the destination file. The string in the middle could stop the loop.

You can use the cat command to open a heredoc session. As you read above, the cat command with redirection first points to the cat with a terminating string. You will redirect your output to a destination file when you terminated the keyword. Then, you can use the most common shell keyboard shortcuts to navigate through your work and type directly into your terminal. By typing your designated terminating string on a line you will end the session. Look at the following syntax:

$ cat << EOF >> example.txt  > Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...  >  > whatever...  >  > you want to type.  >  > EOF  $

While your terminal is waiting for EOF, all you enter would be placed into the destination place. EOF is not part of the file itself and prompt characters are omitted.

Everything you type here will be placed into example.txt when I type EOF on a line by itself. Until then, you can type...    whatever...    you want to type.

I know, you are not going to use heredoc syntax as a substitute for your favorite text editor. Since it is a great quick hack to enter more than one line, more than 10 lines or so start to strain its usefulness. So, you need to trigger your shell’s history function to be able to go up to edit previous lines. However, you can go up, down to recall your text, and move back through your text with Ctrl+B due to your shell and it’s configuration.

Heredoc VS echo

Flexibility is very indispensable while you are working on a shell script. So, from point of this view, heredoc is better than echo. Let’s study an example. While you are writing an installer script to install a set of custom applications automatically. If one of the applications would not distribute with a .desktop file, you can not view it in your Application menu. So, you will generate a .desktop file at install time as a solution. You can use heredoc in your install script instead of writing a .desktop file and carrying it around as an external dependency for your install script. Pay attention to the below syntax:

VERSION=${VERSION:-x.y.z}  PKGNAM=${VERSION:-example}  PKG="${PKGNAM}"-"${VERSION}"-`arch`.tgz
# download package  wget "${PKG}"  tar txvf "${PKG}"
# use here doc to create missing .desktop file  cat << EOF >> $HOME/.local/share/applications/example.desktop  [Desktop Entry]  Version=1.0  Type=Application  Name="${PKGNAM}"  Comment="${PKGNAM}"  Exec="${PKGNAM}" %F  EOF
# insert the rest of an install script...

In this way, while no text editor involves, you have entered text into a file automatically. What the resulting .desktop file look like? As shown below:

[Desktop Entry]  Version=1.0  Type=Application  Name=example  Comment=example  Exec=example %F

Due to the above information, you will be able to use variables within the heredoc while they are correctly solved. Since the EOF only signals the end of the heredoc, it does not appear in the file.

Recommended Article: Introducing and use Heredoc on Ubuntu, Centos, Debian


In this article, Heredoc was introduced to you and you read about how to use heredoc in Ubuntu, CentOS, and Debian. For quick notes and shell scripts, you can use heredoc.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way


7 days a week, 24 hours a day