- STDIN: The standard input stream that accepts text input.
- STDOUT: The standard output stream that displays text output on your screen.
- STDERR: The standard error stream that shows error messages.
cat command:
Redirecting Standard Output and Error
To redirect the standard output to a file instead of the screen, use the forward arrow symbol (>). This operator will overwrite the file with the new output. If you prefer appending the output to an existing file, use the double forward arrow symbol (>>). For instance, to save your shell information and then append a descriptive message, execute the following commands:
2>>:
If you want to run a command without displaying error messages on the screen, you can redirect STDERR to
/dev/null. This special file discards any input provided to it.Using Pipes to Link Commands
Often, you might want to pass the output of one command directly to another without creating an intermediary file—that’s where piping comes into play. The pipe operator (|) transfers the standard output from the command on its left to the standard input of the command on its right. For example:
sample.txt with the following contents:
sample.txt using grep and redirect the output to a file, you will experience:
less for quick viewing:
The tee Command
Another valuable tool for managing IO redirection is thetee command. Unlike the redirection operator that sends the output exclusively to a file, tee duplicates the output by writing it both to a file and to the screen simultaneously. For example, instead of redirecting the output to a file, you can use:
-a option with tee:
Quick Reference Table
| Technique | Purpose | Example Command |
|---|---|---|
| Redirect STDOUT | Send command output to a file | echo $SHELL > shell.txt |
| Append STDOUT | Append command output to an existing file | echo $SHELL >> shell.txt |
| Redirect STDERR | Send error messages to a file | cat missing_file 2> error.txt |
| Append STDERR | Append error messages to an existing file | cat missing_file 2>> error.txt |
| Use Pipes | Pass output from one command as input to another | grep Hello sample.txt | less |
| Utilize tee Command | Duplicate output to file and screen simultaneously | echo SHELL | tee -a shell.txt |