Reading from a file with <
The operator < connects a file to standard input. The command does not see a file name, only a stream of bytes from slot 0. Many programs accept a file argument as well, but programs that only read standard input, such as tr and wc in its default form, need redirection to read a file at all.
Redirecting input is the cleanest way to read a file without the program knowing its name. The count below is the same whether wc is given the name or the content through slot 0. The difference is only that the second form leaves no file name in the output.
printf 'a\nb\nc\n' > letters.txt
wc -l < letters.txt
wc -l letters.txtThe first command prints a bare 3, because wc received only the number of lines and had no name to print. The second prints 3 letters.txt, because it was given the file name. Scripts often use the bare form, since it is easier to capture.
Here-documents
A here-document is a block of text that becomes standard input for a command. It starts with << followed by a delimiter word, and it ends at a line that holds only that word. Everything between is passed to the command. Here-documents make a script self-contained, because a configuration template or a SQL snippet can live next to the code that uses it.
cat <<EOF
hello from a here-doc
the second line
EOFThe output is the two lines between the markers, printed by cat. The text is written out exactly as typed, but with one important caveat: unless the delimiter is quoted, the shell expands variables and command substitutions inside the block before the command sees them.
Expansion inside a here-document
Unquoted delimiters allow expansion, and quoted delimiters turn it off. Quoting can be done by writing the delimiter in single quotes, double quotes, or with a backslash in front. This choice changes the meaning of every $ inside the block.
name=Ada
cat <<EOF
hi $name
EOF
cat <<'EOF'
hi $name
EOFThe first block prints hi Ada, because $name was expanded. The second prints hi $name literally, because the delimiter was quoted. Use the quoted form when the text contains shell syntax you want to keep intact, such as a template with dollar signs for another tool.
With <<-, bash removes leading tab characters from each line of the block, so you can indent a here-document inside a function. The delimiter line may also be indented with tabs. Spaces are not removed.
Here-strings
A here-string is a shorter form for a single line of input. The operator <<< takes one word, expands it, and feeds it to standard input with a newline added at the end. It is the right choice when the input is a value you already have in a variable.
read -r first rest <<< "alpha beta gamma"
echo "$first|$rest"The output is alpha|beta gamma. The read builtin read one line from standard input, split it on whitespace, and stored the first word in first and the remainder in rest. A here-string avoided writing a temporary file.
Reading a file once, one line at a time
A file used as standard input has one offset, and every reader shares it. When a loop reads a file line by line, each read consumes the next line and the offset advances. When the loop finishes, the offset is at the end, and a second loop over the same redirection reads nothing unless you reopen the file. Reopening is the simple fix: each redirection opens the file again from the beginning.
This also explains why a command given a redirection in a loop body behaves differently from one given it once. The redirection on the loop applies to all iterations together, while a redirection inside the body applies to each iteration. Choose the placement deliberately, because a misplaced redirection is one of the more confusing bugs to track down.
printf 'x\ny\n' > two.txt
while read -r a; do echo "first: $a"; done < two.txt
while read -r b; do echo "second: $b"; done < two.txtBoth loops print both lines, because each redirection opens two.txt from the start.
Feeding a program that reads a prompt
Here-documents are also a clean way to answer a program that normally reads a line at a time from a terminal. The program reads each line from slot 0 exactly as if you had typed it, so you can script an interactive session in a repeatable way. This works for tools that read standard input without checking whether it is a terminal.
while read -r line; do echo "got: $line"; done <<EOF
first
second
EOFThe loop prints got: first and got: second. The here-document is attached to the whole while loop, so every read inside the loop consumes the next line of the block.
Hands-on: write a template
Create a small configuration file with a here-document, using a quoted delimiter so that the placeholder stays literal. Then check the file with cat.
cat > settings.conf <<'END'
host=${HOST}
port=8080
END
cat settings.confThe file contains the literal line host=${HOST}, because the delimiter was quoted. Notice that the redirection to settings.conf and the here-document both belong to the same command, cat.