Search notes:

Bash: here document

Creating a file

Combining cat with a here document allows to create a file:
cat > /tmp/tq84.file << EOF
Lorem ipsum dolor sit amet, consectetur
adipiscing elit.
EOF
Github repository about-Bash, path: /scripts/here-document/cat-to-file

Reading lines from a here document

Similarly, lines can be read with read and a while loop to iterate over the lines of a here document:
#!/bin/bash
while read line; do
  echo "line = $line"
done << EOF
first line
second line
third line
EOF
Github repository about-Bash, path: /scripts/here-document/while-read

Suppress leading tabulators

If the limit string starts with a hyphen, leading tabulators are suppressed.
Thus, it is possible to create here document with that look nicer.
#!/bin/bash

cat <<-EOF
  All lines in this here document
  start with spaces.
  
  The dash in front of the EOF
  does not remove them.
EOF

echo -e "\n---------------\n"

cat <<-EOF
	The lines in this here document
	start with a tabulator.
	
	The dash in front of the EOF
	removes them.
EOF
Github repository about-Bash, path: /scripts/here-document/suppress-leading-tabs

See also

bash
A here string is similar to a here document.

Index