$_ CLI In Depth
Learning progress 0%
lesson streams redirection pipes

Input Redirection, Here-Docs & Here-Strings

5 min read Module 7: Streams, Redirection & Pipes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:15

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.

BASH3 lines
printf 'a\nb\nc\n' > letters.txt
wc -l < letters.txt
wc -l letters.txt

The 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.

BASH4 lines
cat <<EOF
hello from a here-doc
the second line
EOF

The 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.

BASH7 lines
name=Ada
cat <<EOF
hi $name
EOF
cat <<'EOF'
hi $name
EOF

The 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.

Note

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.

BASH2 lines
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.txt

Both 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.

BASH4 lines
while read -r line; do echo "got: $line"; done <<EOF
first
second
EOF

The 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.

BASH5 lines
cat > settings.conf <<'END'
host=${HOST}
port=8080
END
cat settings.conf

The 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.

Redirections change the file descriptor table

Visualizer

Redirections are applied left to right, before the command runs. >file 2>&1 and 2>&1 >file look alike, but they send stderr to different places.

1 / 4
Before the command
0/dev/pts/0
1/dev/pts/0
2/dev/pts/0
$ ls /nope >out.txt 2>&1

Bash starts with fds 0, 1 and 2 all pointing at the terminal. Redirections change this table before ls runs, and ls inherits whatever the table says.

ls /nope >out.txt 2>&1; echo "status $?"; cat out.txt
Terminal lab · 3 steps · real Linux in your browser

Feed a command from a block

Write a config with a here-document, read a here-string, and count with input redirection.

01
Write a template with a quoted delimiter
Create config.txt with two lines. Quote the delimiter so that the placeholder stays literal: ``bash cat > config.txt <<'END' host=${HOST} port=8080 END ``
Show hint for step 1
The quotes around END stop the shell from expanding the dollar sign.
02
Split a value with a here-string
Read the words of a string into two variables with a here-string, then save the first word: ``bash read -r first rest <<< "alpha beta gamma" echo "$first" > first.txt ``
Show hint for step 2
read with two names stores the first word in the first name and the rest in the second.
03
Count lines without the file name
letters.txt has three lines. Count them with input redirection and save only the number: ``bash wc -l < letters.txt > count.txt ``
Show hint for step 3
With < the file name never reaches wc, so the output is just the number.
Check yourself Question 1 of 4
Concept Check What a here-string does recall

What does the operator <<< do?

Recap & Key Takeaways

A < redirection makes a file the standard input of a command, so the program reads bytes without knowing the file name.
A here-document supplies a block of text as standard input, and it ends at a line that matches the delimiter.
An unquoted delimiter allows $ expansion inside the block, and a quoted delimiter keeps the text literal.
A here-string with <<< feeds one expanded word to standard input and adds a newline.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue