Redirecting Input & Output Streams
Shell·3 min read·Jan 1, 2025
By default, all the commands executed in a shell read their input from and write their output to the terminal.
However, it is sometimes useful to be able to change this behavior so that commands can read from and write to files instead of the terminal.
This can be achieved using a feature called input/output redirections.
A redirection is the action of capturing the output from a file, a program, or a script and sending it as input to another file, program, or script.
These redirections are mostly used for reading the content of a file, saving the output of a command into a file, or even chaining multiple commands together to create a data processing pipeline.
Standard streams
A stream is a communication channel that transfers data between a program and its environment, like for example the shell and its terminal.
In Unix-like operating systems, there are three types of streams called standard streams:
-
The standard input, often abbreviated stdin, that programs can read data from.
-
The standard output, often abbreviated stdout, that programs can write data to when a successful operation was performed.
-
The standard error, often abbreviated stderr, that programs can write data to when something goes wrong during their execution.
Unlock the CLI & Scripting with Bash module
Learn how to gain advanced control over the OS and automate complex routine tasks prone to manual errors with the CLI and Bash scripting.
You get immediate access to:
- 34 focused lessons across the CLI and Bash
- 4 real-world projects with commented solutions
- Ongoing updates to this bundle
- Lifetime access to this bundle