Bash - Here Documents

1 - About

Language - Here Document in Bash.

Here document in Bash is implemented through redirection. A 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.

3 - Format

The format of here-documents is:



  • « is the redirection operator
  • - (optional) is used 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.
  • wordDelimiter and wordDelimiter delimit the content of the here-document

3.1 - first WordDelimiter


is performed on the first WordDelimiter.

3.1.1 - Quoted

If the first wordDelimiter is:

In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and .

3.2 - second WorldDelimiter

The second WorldDelimiter is generally the result of quote removal on the first WordDelimiter.

4 - Example

4.1 - FirstWorldDelimiter not quoted


cat <<-HereDoc

   my Variable is $foo

my Variable is foo

4.2 - FirstWorldDelimiter quoted


cat <<-"HereDoc"

   my Variable is $foo

my Variable is $foo

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap