๐ Redirections
Redirection allows you to control where input comes from and where output goes โ beyond just printing to the terminal.
๐งญ Input/Output Streams
Every process has three standard streams:
| Stream | Number | Direction |
|---|---|---|
| stdin | 0 | Input |
| stdout | 1 | Output |
| stderr | 2 | Errors |
By default: - stdin reads from keyboard - stdout prints to terminal - stderr prints to terminal
But you can redirect any of them.
โก๏ธ Redirecting Output
Send output to a file instead of terminal:
1 | |
Append to a file:
1 | |
Redirect both stdout and stderr:
1 2 3 | |
Redirect only stderr:
1 | |
โฌ ๏ธ Redirecting Input
Feed data into a command from a file:
1 | |
Pass inline text using here-documents:
1 2 3 | |
Useful in scripts:
1 2 3 4 5 | |
๐งช Combining Redirections
You can combine multiple redirections in one line:
1 | |
This separates matched lines (stdout) from potential grep errors (stderr).
Also useful:
1 | |
This sends both output and errors through pipe while saving to a file.
๐งพ Summary
- Redirection lets you reroute I/O streams.
- Common operators:
>,>>,<,<<,2>,&> - Combine with pipes (
|) for powerful workflows. - Always consider where data should flow in automated scripts.
๐ Continue to: Pipelines