Section 1 of 12

1 Before you start

Before you begin, take a few minutes to check what you already know and how confident you feel. You will see the same questions again at the end of the module — this helps both you and us measure what you have learned. Click an option for every question and confidence rating, then click Next to continue.

Pre-test

What is the difference between > and >>?

Pre-test

What does the command ls /usr/bin | wc -l do?

Pre-test

Which command sends the contents of cohort_2026.csv as input to sort?

Pre-test

After running echo 'hello' > log.txt and then echo 'world' > log.txt, what is in log.txt?

Pre-test

What is standard output (stdout)?

Pre-test

What does the | (pipe) symbol do between two commands?

Pre-test

Which command appends a line to results.txt without erasing what is already there?

Pre-test

What does running > listing.txt on its own (with no command) do?

Pre-confidence

I can save the output of a command to a file with > and append to one with >>.

Not at all confident
Fully confident
Pre-confidence

I can read the output of one command into the input of another using a pipe.

Not at all confident
Fully confident
Pre-confidence

I can explain why doing both > and < on the same file in one command is dangerous.

Not at all confident
Fully confident
Section 2 of 12

2 Introduction

Every command so far has printed straight to your terminal. The real power of the shell is that you can take the output of one command and feed it somewhere else - into a file, or into another command. This lesson covers the four operators that make that possible.

  • Sending output to a file: > (overwrite) and >> (append).
  • Sending a file in as input: < .
  • Chaining commands: | (pipe).
Redirection and pipes re-point a command's default input (your keyboard) and output (your screen), sending output into a file with > or >>, drawing input from a file with <, or feeding one command's output into the next with |.
Redirection and pipes re-point a command's default input (your keyboard) and output (your screen), sending output into a file with > or >>, drawing input from a file with <, or feeding one command's output into the next with |.
Section 3 of 12

3 Understanding stdout - Where Output Goes By Default

Every command has three default channels: standard output (stdout) for normal results, standard error (stderr) for error messages, and standard input (stdin) for what you type. By default stdout and stderr both go to your terminal, and stdin reads the keyboard.

When you redirect with > or |, you change where stdout goes. Errors still come to your screen - we cover stderr in the next lesson. For now, picture every command as a tap pouring its output onto your terminal floor unless you tell it otherwise.

Section 4 of 12

4 Sending Output to a File - > and >>

By default, the commands you run in the terminal will print their results straight to your screen. By using the > and >> redirection operators, you can tell the shell to capture that output and send it directly into a text file instead. This is incredibly useful for saving command results, creating logs, or building data files on the fly.

Section 4.1 of 12

4.1 > overwrites

> filename at the end of a command sends the output into that file instead of your terminal.

Note: If the file already exists, it is wiped first.

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ ls -lh > listing.txt
$ cat listing.txt
Section 4.2 of 12

4.2 Empty trick - > with no command

> filename on its own empties the file (or creates it if it does not exist):

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ > listing.txt
$ cat listing.txt
Section 4.3 of 12

4.3 >> appends

>> adds to the end instead of overwriting:

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ echo 'first line' > diary.txt
$ echo 'second line' >> diary.txt
$ cat diary.txt

Use >> whenever you are accumulating output across multiple commands - logs, results, growing lists.

The > operator writes a command's output into a file and replaces whatever was there, >> adds to the end instead, and a bare > with no command empties the file.
The > operator writes a command's output into a file and replaces whatever was there, >> adds to the end instead, and a bare > with no command empties the file.

Be careful. > vs >> - one character, very different result. > destroys; >> adds. Pause before pressing

Section 5 of 12

5 Reading Input From a File - <

The mirror operator < sends a file as input to a command that would normally read from the keyboard.

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ wc -l < cohort_2026.csv

You will see < less often than > because most commands also accept a filename directly. wc -l cohort_2026.csv does the same thing as wc -l < cohort_2026.csv. The < form is useful in scripts and pipelines where a command only knows how to read stdin.

The < operator redirects a file into a command's standard input, so the command reads the file's bytes exactly as if they were typed at the keyboard and never receives the filename itself.
The < operator redirects a file into a command's standard input, so the command reads the file's bytes exactly as if they were typed at the keyboard and never receives the filename itself.

The < operator redirects a file into a command's standard input, so the command reads the file's bytes exactly as if they were typed at the keyboard and never receives the filename itself.

Section 6 of 12

6 Pipes - | - The Heart of the Shell

The pipe | connects the stdout of one command to the stdin of the next. It lets you chain small commands without ever writing an intermediate file. This is the philosophy bash was built on: many small tools, each good at one job, glued with pipes.

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ ls /usr/bin | wc -l

Read left to right: list /usr/bin, send the list into wc -l, which counts lines. The answer falls out as a single number, no intermediate file on disk.

A pipe sends one command's output straight into the next command's input as a live stream, so tools chain together without any file being written to disk.
A pipe sends one command's output straight into the next command's input as a live stream, so tools chain together without any file being written to disk.

Pipes can be as long as you like. Four or five stages is normal. Each stage should do one thing, and the output of each stage should be readable on its own when you are debugging.

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ cat cohort_2026.csv | head -n 1
$ cat cohort_2026.csv | tail -n +2 | wc -l

The second command says: read cohort_2026.csv, skip line 1 (tail -n +2 starts from line 2), then count what remains. That is how you count data rows in a CSV without manually subtracting the header.

Section 7 of 12

7 Combining Redirection and Pipes

Finish a pipeline by redirecting the last command into a file:

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ ls /usr/bin | wc -l > tool_count.txt
$ cat tool_count.txt

Or start a pipeline from a file with <:

Try it out

Try this snippet in the Bash Scratchpad on the right.

Try this snippet
$ ls /usr/bin > tool_names.txt
$ ls /usr/bin >> tool_names.txt
$ sort < tool_names.txt | uniq > unique_tools.txt
$ wc -l < unique_tools.txt

Read this as: appending the listing a second time leaves tool_names.txt with 1428 lines and every tool name duplicated. sort brings the identical names next to each other, uniq collapses each adjacent pair to a single copy, and you save the clean 714-line list.

uniq only removes duplicate lines that are adjacent, so sorting first is what brings every repeat together and lets the doubled 1428 line listing collapse back to 714 unique tool names.
uniq only removes duplicate lines that are adjacent, so sorting first is what brings every repeat together and lets the doubled 1428 line listing collapse back to 714 unique tool names.
Section 8 of 12

8 Check your understanding

You have reached the end of the module. Try the same questions again — your answers here, paired with your pre-test answers, are how we measure what the module taught you. Answer every question and confidence rating, then click Submit and see results to view your score.

Post-test

What is the difference between > and >>?

Post-test

What does the command ls /usr/bin | wc -l do?

Post-test

Which command sends the contents of cohort_2026.csv as input to sort?

Post-test

After running echo 'hello' > log.txt and then echo 'world' > log.txt, what is in log.txt?

Post-test

What is standard output (stdout)?

Post-test

What does the | (pipe) symbol do between two commands?

Post-test

Which command appends a line to results.txt without erasing what is already there?

Post-test

What does running > listing.txt on its own (with no command) do?

Post-confidence

I can save the output of a command to a file with > and append to one with >>.

Not at all confident
Fully confident
Post-confidence

I can read the output of one command into the input of another using a pipe.

Not at all confident
Fully confident
Post-confidence

I can explain why doing both > and < on the same file in one command is dangerous.

Not at all confident
Fully confident
Section 9 of 12

9 Your results

Here is how your post-test answers compare with your pre-test answers. The pre/post pairing is the most reliable way to see what this module actually taught you.

Your score

Submit the post-test to see your results.

Muddiest point

What is the one thing from this module that is still unclear to you?

Rate this module

Overall, how would you rate this module?

How likely are you to recommend this module to a peer? (0 = not at all, 10 = extremely likely)