here document
A way to write several lines of text straight into a script or file, opened with << and a marker word such as EOF and closed by that word on its own line.
In more detail
In a shell, the text becomes the command's standard input, and variables inside are expanded unless the marker is quoted, as in << 'EOF'. The <<- form lets the text be indented: shells strip leading tabs, and HCL strips the common leading indentation. The name comes from shells, where the text is fed to a command from right here in the script.
Examples
cat << EOF
Dear $name,
Your report is ready.
EOFdocker exec -i broker sh -c 'cat > /tmp/v1.json' << 'EOF'script = <<-EOT
line one
line two
EOTRelated terms
- here string — The bash form <<< word, which feeds a single string to a command as its standard input.
- end of file — The point where input runs out; also, by convention, the word EOF used to end a here document.
- redirection — Sending a command's output into a file instead of the terminal: > replaces the file, >> appends to the end of it.
- interactive terminal — The -it flags that connect your keyboard to a container and give it a terminal, so you can use a shell inside it.
- JSON Schema — A standard vocabulary for describing the allowed shape of JSON data: which fields exist, their types, which are required and what values are allowed.
- string interpolation — Building a string from a template whose placeholders hold expressions, which are evaluated and inserted as text.
- HCL — HashiCorp Configuration Language: the block-based configuration syntax of Terraform files, which end in .tf.
Taught in
- Shell Variables, The Environment And export — Bash Scripting & Command Line Automation
- Script Input And Output: read, Heredocs, printf — Bash Scripting & Command Line Automation
- The Schema Registry — Apache Kafka & Event Streaming
- Compatibility Modes — Apache Kafka & Event Streaming
- Writing Your First Configuration In HCL — Infrastructure as Code with Terraform
- Expressions And Functions: Making Configuration Compute — HashiCorp Terraform Associate (004) Exam Prep
References
- Bash Reference Manual: Here Documents
- POSIX shell command language: here-document
- bash(1) manual page
- Strings and templates: heredoc strings