Guides Shell Scripts and Here Documents

Shell Scripts and Here Documents

A here document (or heredoc) is a way of getting text input into a script without having to feed it in from a separate file. If you’ve got a small quantity of data you don’t expect to change often, it’s a quick and tidy way of keeping script and data together. For more complicated data or scripts, separating the two is still usually a better idea.

Tip of the Trade: A here document, or heredoc, is one way to get text input into a script without feeding it from a separate file. If the data or script is complicated, however, keeping the two separate is generally a better idea.

The basic version of this in bashlooks like this:


which will output

several lines of 
my data
listed here

You can use any label (although EOF is standard) — just make sure it matches at both start and end.

There are a couple of useful variations on this format. Just using a plain label, as above, means the shell will interpret any special characters. This would substitute your current directory for $PWD:


However, with some data sets this can cause problems, especially if you don’t remember that it happens! To disable interpretation, use single or double quotes:


This will output $PWDverbatim:

current directory variable is:

By and large, it’s best to disable shell interpretation by default and to take the quotes out if your data definitely requires it.

Another useful trick is to use a minus sign after the . This will ignore tab characters (Note: not all whitespace!) at the start of a line, so you can indent your data to increase the readability of your script:


You can also use heredocs in other programming languages, including Perl, PHP and Python. Be aware, however, that quotes may affect escaping of special characters in different ways in different languages.

Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of “Linux System Administration Recipes: A Problem-Solution Approach” (Apress, 2009).

Follow ServerWatch on Twitter

Latest Posts

How to Convert a Physical Computer to a Virtual Machine

Many organizations are implementing virtualization technology into their networks to convert physical computers to virtual machines (VM). This helps reduce overall physical hardware costs,...

HPE ProLiant DL380 Gen10: Rack Server Overview and Insight

The HPE ProLiant DL380 series has consistently been a market leader in the server space. The Gen10 released in 2017 further increased HPE's market...

Best Server Management Software & Tools 2021

Finding the best server management software tools for your organization can have a major impact on the success of your business operations. Manually handling...

IBM AS/400: Lasting the Test of Time

Some server operating systems (OS) were built to survive the test of time – the IBM AS/400 is one such system.  The AS/400 (Application System/400)...

What is Disaster Recovery?

The modern organization's heavy dependence on using data to drive their business has made having a Disaster Recovery (DR) plan in place a necessity....

Related Stories