|
|
|
Here-documents (heredocs) let you include multi-line text directly in your script. They’re great for embedding configuration files, SQL queries, or help text. |
|
|
Basic syntax: < |
|
|
Use <<‘EOF’ (quoted) to prevent variable expansion. |
|
|
Use <<-EOF to strip leading tabs (not spaces). This helps with indentation in scripts. |
|
|
Here-documents are commonly used with commands that read from stdin. |
|
|
Send mail (example - won’t actually send): |
|
|
Create a configuration file: |
|
|
POSIX uses echo and pipe for single-line input: |
|
|
Bash
Here-strings (<<<) provide a shorthand for passing a string directly to a command’s stdin: |
|
|
Here-strings are particularly useful with read: |
|
|
They can include variable expansion: |
|
|
Here-documents work great for multi-line variables. |
|