In-Memory Here Doc
When shell encounters any of these syntaxes:
n<< delimiter
orn<<- delimiter
n<< 'delimiter'
orn<<- 'delimiter'
n<< "delimiter"
orn<<- "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:
Produces:
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.
You can collect the buffered data inside a sub-shell to store it in a variable.
When using the variable to which the data is assigned, quoting it prevents word-splitting from being performed on expansion.
Last updated