Using Here Documents in Linux

Using Here Documents in Linux


This post was last updated on June 3rd, 2020 at 04:38 pm

cat <<EOF>/tmp/newfile

This is the contents of the here document
It can span multiple lines
A here document says to take all of this text up to the terminating
characters (which in this case are the characters E, O, and F.
I can chose any character string I want to. As soon as the script sees
these characters at the beginning of a line, all by itself with no
trailing whitespace, it will then terminate creating the here document.
Note that I am taking all this text and saving it in the document
called /tmp/newfile. That’s all I have for now, the next line
will contain the magical 3 characters:

Now you can use the new file however you need:
cat /tmp/newfile

Here is a copy of the man page on here documents. You can bring this up
on your system by looking up “Here Documents” in the “bash” man page
(man bash, then type /Here Documents):

Here Documents This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen. All of the lines read up to that point are then used as the standard input for a command. The format of here-documents is: <<[-]word here-document delimiter No parameter expansion, command substitution, arithmetic expansion, or pathname expansion is performed on word. If any characters in word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, com- mand substitution, and arithmetic expansion. In the latter case, the character sequence \ is ignored, and \ must be used to quote the characters \, $, and '. If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.
Previous Installing Linux on a Compaq PL2500
Next Proliant 1500 and RedHat 6.2

About author

shibaa987 275 posts

Linux kernel developer and a firmware developer with an experience of 10+ years.

View all posts by this author →

You might also like

System Internals 0 Comments

Difference Between MBR And GPT

Large storage disks usually have logical partitions. An Operating System uses a partition table to identify the start address, filesystem information, and other details of a specific logical partition. The

System Internals 0 Comments

The Concept Of RAID

This post was last updated on August 3rd, 2020 at 01:50 pmOriginally the term RAID defined as “Redundant Array of Inexpensive Disks” but now it is referred to as “Redundant

System Internals 0 Comments

Basic Operating System Services: A beginner’s guide

This post was last updated on November 3rd, 2020 at 05:20 pmOperating System Services are the basic functionalities or the responsibilities of an Operating System that it provides to the


No Comments Yet!

You can be first to comment this post!

Leave a Reply