Shell (Bash) Tutorial

I/O Redirection

Pipes, Standard Streams, and redirecting data in the shell

Standard Streams

Every program in the shell starts with three standard "Files" open:
  1. stdin (0): Standard Input (keyboard or data from another program).
  2. stdout (1): Standard Output (the terminal screen).
  3. stderr (2): Standard Error (error messages, also usually the screen).

Redirecting to Files

OperatorActionDescription
>OverwriteSave output to a file (Deletes old content)
>>AppendAdd output to the end of a file
2>ErrorRedirect only error messages
&>AllRedirect BOTH output and errors
bash
ls > files.txt       # Save list to file
ls 2> errors.txt     # Save errors only
ls &> /dev/null      # Silence everything!

Pipes (|)

Wait! This is the most powerful concept in the shell. A Pipe takes the stdout of the first command and "pipes" it as the stdin of the second command. This allows you to chain small, simple tools together to solve complex problems.
bash
# Find all processes, search for 'node', and count them
ps aux | grep node | wc -l

Input Redirection (<)

You can tell a command to read its input from a file instead of the keyboard.
bash
wc -l < data.txt

Summary

TermDescription
****
>Redirecting output to a file
<Redirecting input from a file
2>&1Sending errors to the same place as standard output
/dev/nullThe "Black Hole" - discard all output
teeSend output to a file AND the screen at the same time
Best ForBuilding complex data processing pipelines
Logic"Small tools connected by pipes"
SafetyRedirect errors (2>) to logs for debugging
ModernEssential for managing cloud logs and CI/CD pipelines
StandardPart of the fundamental Unix philosophy
IdentityThe "Pipe" logo is often the symbol for shell power