Web Analytics

Here Documents & Here Strings

Intermediate~20 min read

Use heredocs for multi-line input blocks and here strings for quick one-liners.

Heredoc Variants

Output
Click Run to execute your code
Note: Quote the delimiter (<<'EOF') to disable variable and command expansion.
Pro Tip: Use <<- to strip leading tabs for nicely indented scripts.
Caution: The delimiter must appear alone on its lineโ€”no leading/trailing spaces.

Common Mistakes

1) Indentation with spaces

# Wrong (spaces won't be stripped)
cat <<-EOF
  indented with spaces
EOF

2) Unintended expansion

# Wrong (variables expand)
cat <

Exercise: Template

Task: Print a multi-line message that includes $USER and the current date using a heredoc.

Output
Click Run to execute your code
Show Solution
cat <

Summary

  • Use heredocs for multi-line input.
  • Quote delimiter to disable expansion.
  • Use here strings (<<<) for short inputs.

What's Next?

Connect commands without temp files using Process Substitution.