Serious Shell Programming
  • Introduction
  • Acknowledgements
  • Basics
    • Strings
      • Single-Quotes
      • Double-Quotes
      • Unquoted Strings
      • Compound Strings
    • Here Documents
      • Here Doc
      • Indented Here Doc
      • Literal Here Doc
      • In-Memory Here Doc
    • Conditionals
      • Built-in test
      • Parameter Conditionals
      • Parameter test
    • Regex
      • grep
      • awk
      • pcre
    • Control Flow
      • Binary Operators
      • if-elif-else
      • case Statement
      • for Loop
      • while Loop
      • Functions
  • shellcheck
    • Introduction
    • Bad Advice
  • Style
    • awk
    • case
    • Redirection
    • Comments
    • trap
  • String Functions
    • substr
    • sprintf
    • replace
    • replaceall
    • replacestart
    • replaceend
    • fnmatch
  • awk
    • Pre-declaring Arrays
    • Sorting Arrays
  • Know Your limits
    • Arguments
    • Environment Variables
    • Solutions
Powered by GitBook
On this page

Was this helpful?

  1. Basics
  2. Here Documents

In-Memory Here Doc

When shell encounters any of these syntaxes:

  • n<< delimiter or n<<- delimiter

  • n<< 'delimiter'or n<<- 'delimiter'

  • n<< "delimiter"or n<<- "delimiter"

where n is a number (0-9), it starts creating a multi-line buffer to be sent to file descriptor n.

When you combine that with the exec builtin, you can buffer input to a file descriptor. For example:

1 #!/bin/sh
2 exec 3<< EOF
3 Buffered data
4 EOF
5 cat <&3

Produces:

Buffered data

The above example uses file descriptor 3 because 0-2 are reserved in POSIX for stdin, stdout, and stderr respectively.

An in-memory here-doc can be combined with the indented here-doc.

1 #!/bin/sh
2 if : true; then
3     exec 3<<- EOF
4     Buffered line 1
5     \    Buffered line 2
6     EOF
7     awk 'sub(/^\\\t/,"\t")||1' <&3
8 fi

You can collect the buffered data inside a sub-shell to store it in a variable.

1 #!/bin/sh
2 exec 3<< EOF
3 Buffered data
4 EOF
5 data=$( cat <&3 )
6 echo "$data"

When using the variable to which the data is assigned, quoting it prevents word-splitting from being performed on expansion.

PreviousLiteral Here DocNextConditionals

Last updated 5 years ago

Was this helpful?