🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Pipes

A pipe feeds the output of one command straight into the next command. The pipe sign is |.

Pipe into grep

cat prints notes.txt, and grep keeps only the line with Ana.

cat notes.txt | grep Ana

Pipe into head

ls lists the photos, and head keeps only the first two lines.

ls photos | head -2

A chain of three

head keeps the first three lines, and tail keeps the last of those.

cat notes.txt | head -3 | tail -1

Pipe into grep from find

find lists the photo files, and grep keeps the beach one.

find photos -type f | grep beach

Example

ls photos | grep jpg
cat notes.txt | head -2 | tail -1

Key points

  • Use | to join two commands, so the second reads the first one's output.
  • Each command in a pipe does one small job.
  • A pipe can join three commands, or more, in a chain.
  • The last command in the chain prints the final result.

In this example, the first pipe lists the photos and keeps the jpg names. The second pipe prints notes.txt, keeps the first two lines, and then keeps the last of those.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does the pipe sign | do?