Typorithm · medium

Pipes & Redirection

bash-pipesbashpiperedirectstderr
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Pipes & Redirection

0%

Text Processing

| pipes one command's stdout into the next command's stdin to compose filters. > redirects stdout to a file (truncating it), >> appends, and 2> redirects stderr separately — here to /dev/null to discard it.

A pipe (|) connects the stdout of the command on its left to the stdin of the command on its right, letting you chain small filters into a pipeline. cat access.log | wc -l streams the file straight into the line counter.

Redirection sends a stream to a file instead of the terminal. > overwrites (truncates) the target, while >> appends to whatever is already there — so echo "hi" > out.txt then echo "more" >> out.txt leaves two lines.

Streams are numbered: 1 is stdout and 2 is stderr. 2> redirects only the error stream, so grep error log.txt 2> /dev/null shows matches but silently drops any error messages by sending them to /dev/null.

01

| sends stdout into the next command's stdin.

02

> truncates a file; >> appends to it.

03

2> redirects stderr (e.g. to /dev/null to discard).

cat access.log | wc -l
sort names.txt | uniq -c
echo "hi" > out.txt
echo "more" >> out.txt
grep error log.txt 2> /dev/null

Read the implementation

Connect each part of the code to the concept before you type.

1. Pipe to wc

| feeds cat's stdout into wc -l, which counts the lines flowing through.

cat access.log | wc -l

2. Count occurrences

sort groups equal lines together so uniq -c can count each run — the classic counting combo.

sort names.txt | uniq -c

3. Redirect stdout

> writes echo's stdout to out.txt, truncating the file first.

echo "hi" > out.txt

4. Append stdout

>> appends to out.txt instead of truncating it.

echo "more" >> out.txt

5. Discard stderr

2> redirects file descriptor 2 (stderr); sending it to /dev/null throws error messages away.

grep error log.txt 2> /dev/null

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Pipes & Redirection: Lesson & Practice · Typorithm | ApplyDjinn