Skip to content

Exercise 8 of 10 · Files and redirection

Cabin Guest Book

What you will make

A guest book built up in guests.txt, then printed back with cat, counted with wc -l, and turned into three personalised welcome lines by a while read loop.

The one new idea: > creates a file, >> appends to it, and while read ... done < file reads it line by line

Redirection is what makes the shell a tool for gluing programs together: any command's output can become a file, and any file can become a command's input. The > versus >> distinction is the difference between keeping a log and wiping it every run, and while read ... done < file is the standard way to process a file line by line in bash.

Go straight to the code ↓

Putting text in a file and getting it back

The > after a command sends its output into a file instead of the screen. If the file exists, > empties it first and then writes — every time. To add to the end instead, use >>. So a script that builds a list line by line starts with one > and continues with >>.

Files here live in a small in-memory filesystem that exists only for the length of the run, so you can write freely; nothing on your computer is touched.

cat prints a file. wc -l counts its lines, and wc -l < file prints just the number, with no filename after it — the < hands the file to wc as its input.

The same < is how a loop reads a file one line at a time:

Shell
while read -r line; do
  echo "- $line"
done < list.txt

read takes one line and puts it in the variable; when the file runs out, read fails and the loop stops. The -r tells read to keep backslashes as they are. The < list.txt goes after done, because it feeds the whole loop, not just one command inside it.

A worked example

Shell
echo "Bring a torch" > list.txt
echo "Bring a map" >> list.txt
echo "Lines: $(wc -l < list.txt)"
while read -r line; do
  echo "- $line"
done < list.txt
Output
Lines: 2
- Bring a torch
- Bring a map

The first echo creates the file, the second appends, and the loop reads both lines back.

Your turn

The editor holds a guest book for a cabin. Three names are written to guests.txt, then the script prints the book, counts the guests, and prints a welcome line for each.

Two lines are marked. The second name is written with > instead of >>, so it wipes out the first name — the book ends up with two guests instead of three. The loop's done line is missing the <, so a bare word right after done is a syntax error, and the script stops before the loop can print a single welcome line.

Fix both. You should see all three names in the book, a count of 3, and three welcome lines.

If something goes wrong

If the first guest is missing from the book, one of the echo lines is still using >. Only the very first write should use >.

A syntax error pointing at guests.txt means the redirect after done is still missing its <.

If the loop prints nothing, check the < is after done, not somewhere inside the loop body.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Using > for every line when building a file
> truncates the file to nothing before each write, so only the last line survives. Start with > once, then use >> for everything that should be added to the end.
Putting the < redirect inside the loop body or on the read line
read < guests.txt would reopen the file on every pass and read the first line forever. The redirect belongs after done, where it feeds the whole loop once.
Leaving out the -r on read
Without -r, read treats backslashes in the line as escape characters and drops them. These names have none, so the output would still match, but -r is the habit worth building because it makes read leave lines alone.
Expecting wc -l guests.txt to print just the number
With a filename argument, wc prints the count followed by the filename. Feeding the file through < instead gives wc no filename to print, which is why the script uses wc -l < guests.txt.

Want a blank editor instead? Open the Bash & the Shell playground.