Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Connecting commands with pipes and redirection

Commands do not have to work alone. The shell can connect their input and output to files or to one another, letting you build a larger job from small commands you already know.

Three standard streams

So far, every command has taken what you typed and printed its answer on the screen. That arrangement is more flexible than it looks. When the shell starts a program, it gives the program three connections, called standard streams, and the program uses them without caring where they lead:

StreamShort nameNumberUsed forConnected by default to
Standard inputstdin0Data the program readsThe keyboard
Standard outputstdout1The program's resultsThe screen
Standard errorstderr2Error messages and progress notesThe screen

Output and errors both land on the screen, so they look the same. Ask ls about one file that exists and one that does not, and you get one line on each stream:

$ ls notes.txt missing.txt
ls: cannot access 'missing.txt': No such file or directory
notes.txt

Because the two lines travel on different streams, the shell can send them to different places: either stream can go to a file instead of the screen, standard input can come from a file or from text written into the command line, and one command's output can feed another command. The shell connects the streams before the program starts, so the same idea works with almost every command.

Sending output to a file

The > character tells the shell to connect standard output to a file instead of the screen. Nothing appears on the screen; the file receives the output instead:

head -n 5 app.log > first-lines.txt
echo "Checked on Monday" >> first-lines.txt

A single > creates the file if it does not exist and empties it if it does, so the file ends up holding only this command's output. A double >> appends: it adds the output to the end and keeps what was already there. That makes >> the right choice for a running log and > the right choice for a fresh result.

The emptying happens first, before the command even starts, and like rm it has no undo. That causes a classic surprise with sort, which prints the lines of a file in order:

sort names.txt > names.txt

The shell empties names.txt to prepare it for the output, then starts sort, which finds an empty file to sort. The result is an empty file. Write to a new name instead, and replace the original with mv once you have checked the result.

Reading input from a file

Many commands that take a file name also work without one. Give sort no file and it reads standard input, which is the keyboard. Type a few lines, then press Ctrl+D on a new line to say "that is the end", and sort prints them in order.

The < character connects standard input to a file instead. The difference is easy to see with wc:

$ wc -l app.log
212 app.log
$ wc -l < app.log
212

In the first command, wc opens the file itself, so it knows the name and prints it. In the second, the shell opens the file and hands over its contents through standard input, so wc sees only data and prints only the number. That bare number is handy when you want to use it somewhere else. As before, macOS pads it with spaces on the left.

Writing a file with a here-document

Standard input can also come from lines you write into the command itself. A here-document starts with << and a word you choose to mark the end, often EOF for "end of file". Every line after it, up to a line holding only that word, becomes the command's standard input. With cat and >, that writes a file in one go:

cat > shopping.txt <<'EOF'
bread
apples
coffee
EOF

Read it in two parts. <<'EOF' feeds cat the three lines in between, and > shopping.txt sends what cat prints into the file. The closing EOF must sit alone at the start of its line, with no spaces before or after it, or the shell keeps reading, waiting for the end.

The quotes around the first 'EOF' tell the shell to keep the lines exactly as written. Without them, the shell would treat some characters inside, such as $, as instructions of its own before cat saw them. Quoting the marker is the safe habit whenever the lines are meant to be stored as they are.

This is how the labs write small files without opening an editor: a block that starts with cat > name <<'EOF' can be pasted into the terminal as a whole, and it leaves exactly those lines in the file. It is also a quick way to make a short test file whose right answer you can work out by eye.

Pipes

Redirection connects a command to a file. A pipe, written |, connects one command's standard output directly to the next command's standard input, with no file in between. In Reading and changing files, line 40 of a file was the last line that head -n 40 printed. A pipe finishes the job:

head -n 40 app.log | tail -n 1

head sends lines 1 to 40 into the pipe, and tail, given no file, reads them from its standard input and prints the last one. Each command does one small job, and the pipe joins them into a bigger one. A chain of commands joined this way is called a pipeline.

Pipelines become powerful with commands that filter and arrange lines. grep prints only the lines that contain a pattern, and sort puts lines in order. Suppose a web server writes one line for each request it answers. After the quoted request comes the status code, the server's three-digit summary of how the request went: 200 means it worked, 404 that nothing was found at that path and 500 that the server itself failed. The size of the response comes last:

203.0.113.5 - - [05/Oct/2026:10:03:21 +0000] "GET /photos HTTP/1.1" 200 5120
198.51.100.7 - - [05/Oct/2026:10:03:24 +0000] "GET /albums/9 HTTP/1.1" 404 512

Counting the requests that were answered with 404 takes one pipeline:

grep '" 404 ' access.log | wc -l

The single quotes keep the pattern, spaces and all, together as one argument, and inside them the double quote is an ordinary character. Starting the pattern with the closing quote of the request matches the status column, not a 404 that happens to appear in a path or a size. grep passes the matching lines on, and wc -l counts them. To look at the lines themselves, replace wc -l with head to see the first few. Searching and shaping text covers grep and sort in depth.

A second grep narrows the first one's result, because it reads only the lines the first one passed on. grep ' /albums/' access.log | grep '" 404 ' keeps the requests for album paths that were answered with 404. Building a pipeline this way, one filter at a time with a look at the lines in between, is how you know each filter keeps what you meant.

Keeping errors apart

Errors have their own stream so that results stay clean. When you save output with >, error messages still reach the screen, where you will notice them, instead of hiding inside the file. The same is true of a pipe: it carries standard output only, so an error from a command in a pipeline appears on the screen instead of being counted or searched.

Take ls a b in a folder where a exists and b does not. It writes one line to each stream: the name a on standard output and an error about b on standard error. To redirect standard error, put its number in front: 2>. Standard output's number, 1, is the default, which is why a plain > means 1>. To send both streams to the same file, write > all.txt 2>&1. Read 2>&1 as "send stream 2 to wherever stream 1 goes right now". The shell works through redirections from left to right, so the order matters. In > all.txt 2>&1, standard output already points at the file when errors are told to follow it. Written the other way round, 2>&1 > all.txt joins errors to the screen, because that is where standard output pointed at that moment, and only then moves standard output to the file.

ls a b > out.txt

Standard output goes to
out.txt
Standard error goes to
The screen

ls a b 2> errors.txt

Standard output goes to
The screen
Standard error goes to
errors.txt

ls a b 2> /dev/null

Standard output goes to
The screen
Standard error goes to
Discarded

ls a b > all.txt 2>&1

Standard output goes to
all.txt
Standard error goes to
all.txt

Both lines in all.txt

ls a b 2>&1 > all.txt

Standard output goes to
all.txt
Standard error goes to
The screen

The error is not in all.txt

ls a b | wc -l

Standard output goes to
wc -l
Standard error goes to
The screen

wc counts 1 line

A pipe carries standard output only. The shell applies redirections from left to right.

Each redirection decides where the two output streams of ls a b lead. The order of 2>&1 matters.

/dev/null is a special file that accepts anything written to it and keeps none of it. 2> /dev/null silences errors, which is useful when you expect a harmless message, and risky when you do not, because a real problem disappears with it. When you do want errors in a pipeline, join them first: ls a b 2>&1 | wc -l counts both lines.

The reverse direction is just as useful. >&2 sends a command's standard output to standard error, so a message you print yourself is treated as an error:

echo 'No log file found here' >&2

On the screen it looks like any other line. The difference shows when output is collected. In echo 'No log file found here' >&2 | wc -l, the message skips the pipe and wc counts 0 lines. A file of commands from Reading and changing files, run as bash steps.sh > results.txt, works the same way: its results go into the file and its warnings still reach you on the screen.

Saving a copy with tee

Sometimes you want to keep the lines passing through a pipeline and still pass them on. tee, named after a T-shaped pipe fitting, does both: it copies its standard input to a file and to its standard output.

grep '" 404 ' access.log | tee not-found.log | wc -l

The count appears on the screen, and not-found.log holds the matching lines for a closer look later. Like >, tee replaces the file's contents; tee -a appends, like >>.

A pipeline's exit status

In Terminals and shells, echo $? showed the exit status of the last command: 0 for success, another number for a failure. A pipeline is several commands, but it reports one exit status, and by default it is the status of the last command only:

$ grep '" 404 ' missing.log | wc -l
grep: missing.log: No such file or directory
0
$ echo $?
0

grep failed, and its error reached the screen on standard error. wc received no lines, counted them correctly as 0 and succeeded, so the pipeline as a whole reports success. A && after it would run, and a count of 0 would look like a real answer. When a pipeline's result matters, read the error lines as well as the result, and check that the input exists before trusting a count: here the 0 said nothing about the log, because there was no log to count.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

You run sort names.txt > names.txt to put a list in order. What does names.txt contain afterwards? 1 of 3

Choose one answer.

notes.txt exists and missing.txt does not. You run ls notes.txt missing.txt | wc -l. What happens? 2 of 3

Choose one answer.

You want the output and the errors of ls a b in one file, all.txt. Which command puts both there? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity